C# - how do I work with hex numbers?

Spungo

Diamond Member
Jul 22, 2012
3,217
2
81
This seems like the most basic thing in the world. How do I make C# see hex numbers?
All I'm trying to do is convert 0xFFFFFF (black) to an integer. For the time being, signed or unsigned doesn't matter.

Code:
black = Convert.ToInt32(0xFFFFFF);
Compile error: the expression being assigned to black must be constant.

Code:
black = Convert.ToInt32('0xFFFFFF');
Compile error: cannot implicitly convert type 'string'.

Code:
black = Convert.ToInt32(FFFFFF, 16);
Compile error: name 'FFFFFF' does not exist in the current context. Cannot implicitly convert type 'string'.


The only thing I can find on the internet that resembles this problem is here:
http://msdn.microsoft.com/en-us/library/swz6z5ks(v=vs.110).aspx
Code:
string[] hexStrings = { "80000000", "0FFFFFFF", "F0000000", "00A3000", "D", "-13", "9AC61", "GAD", "FFFFFFFFFF" };

      foreach (string hexString in hexStrings)
      {
         Console.Write("{0,-12}  -->  ", hexString);
         try {
            uint number = Convert.ToUInt32(hexString, 16);
......
Which is pretty much what I'm doing. The second iteration of that loop would be:
Convert.ToUInt32("0FFFFFFF", 16);
Which returns an error: the expression assigned to black must be constant.
 

Mark R

Diamond Member
Oct 9, 1999
8,513
16
81
At coding time:
Code:
int a = 0xFFFFFF;

At run time:
Code:
int a = Convert.ToUInt32 ("FFFF1234", 16);

Your problem is that you have defined "black" as constant. Constants cannot be evaluated at run time, therefore if you have a constant, it can only be assigned at code time.

Get rid of the "const" and it should work. If you really want to make sure your variable can't be changed later, then define it as "readonly" instead of "const".
 

serpretetsky

Senior member
Jan 7, 2012
642
26
101
I want to further add to the answer.

Hexadecimal is not a data type. To specifiy that you want to convert from hexadecimal to int32 doesn't really make sense, there is nothing to convert. 0xFFFFFFFF is already an int32. Internally, the computer stores everything as binary numbers anyways, so regardless if you give it hex, oct, dec, or binary, everything is stored as binary.

As mark pointed out, you can easily write

int somenumber = 0xffffffff;

This is exactly the same thing as writing

int somenumber = 4294967295;

and, since we did not specify unsigned, both of these are the same thing as writing

int somenumber = -1;

Internally, all of those are going to be stored as identical binary values. Now, let's say that instead of having a hex value, you have a string that represents a hex value. 0xffffffff is not the same thing as "0xffffffff". The string must be converted first because it will (typically) be represented as an array of chars (or an array of some other data types).

Usually when you ask a user to enter a hexadecimal value as input, you have asked them to enter a string. In this case you need to convert the string to a value.

Or let's say you want to convert from an int16 to int32. This may seem trivial, but none the less, internally, the computer has to make sure to pad the front 16 digits and preserve the sign as well.

In conclusion: a hex value is not a datatype, it is a numerical system, and the computer doesn't give a damn if you tell it something is hex or decimal, either way the compiler will convert everything to 1's and 0's.
 

Spungo

Diamond Member
Jul 22, 2012
3,217
2
81
Usually when you ask a user to enter a hexadecimal value as input, you have asked them to enter a string. In this case you need to convert the string to a value.
Ah! That clears up some of the confusion. All of the examples I've seen so far involved converting things from Console.ReadLine(), so I assumed converting had to be done.
What I'm trying to do is play around with enumerations. Enum would explain why it's set as a constant.

Thanks guys!