user1327064 - 1 year ago 236
C# Question

# How to convert decimal to hex using c#?

I have a program written in Delphi is converting 11 to 0xB and 28 to 0x1c. I tried to convert 11 (decimal to Hex) in c# using this:-

``````var deciValue01 = 11;
var deciValue02 = 28;
var deciValue03 = 13;
System.Diagnostics.Debug.WriteLine(string.Format("11 = {0:x}", deciValue01));
System.Diagnostics.Debug.WriteLine(string.Format("28 = {0:x}", deciValue02));
System.Diagnostics.Debug.WriteLine(string.Format("13 = {0:x}", deciValue03));
``````

but the results I am getting is:-

• 11 = b

• 28 = 1c

• 13 = d

Wondering how to convert 11 to '0xB' and 28 to '0x1c' and 13 to '0xD'? Isn't it I need to change from Decimal to Hex?

You just need to use `X` to make it capital hex digits instead of lower case, and add the `0x` yourself:
``````// Add using System.Diagnostics; at the top of the file... no need to
Note that the `deciValue01` values are neither "decimal" nor "hex" themselves. They're just numbers. The concept of "decimal" or "hex" only makes sense when you're talking about a textual representation, at least for integers. (It matters for floating point, where the set of representable types depends on the base used.)