By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Variable in Hexadecimal Format in C#

+5 votes

Help me to declare an integer variable and assign it with the value 254 in hexadecimal format (0x##).

I need to print the variable and ensure that the result is “254”. Meaning the output must be the number 254.

asked in C# category by user Jolie Ann

2 Answers

+2 votes
Best answer

First you need to use Windows calculator (use the programmer option) to find 254's hexadecimal representation, which is: FE

It will look like 0xFE in hexadecimal format.

then comes the code:


using System;

class VariableInHexadecimalFormat
    static void Main()
        int a = 0xFE;
answered by user mitko
edited by user golearnweb
+1 vote

Or you can convert it in the placeholder:


answered by user andrew