0

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?

3
  • So you want to add a "0x" prefix? Why not simply add it as a literal? Format("0x{0:X}") Commented Apr 12, 2013 at 18:03
  • And use `"0x{0:X}" to make the hex characters uppercase instead of lowercase, because it sems you want that. Commented Apr 12, 2013 at 18:04
  • What you are getting is exactly right? Just add Ox to the string you generate? Commented Apr 12, 2013 at 19:41

3 Answers 3

2

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
// explicitly qualify all your type names
Debug.WriteLine(string.Format("11 = 0x{0:X}", deciValue01));
Debug.WriteLine(string.Format("28 = 0x{0:X}", deciValue02));
Debug.WriteLine(string.Format("13 = 0x{0:X}", deciValue03));

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.)

Sign up to request clarification or add additional context in comments.

Comments

1

Try This

int value = Convert.ToInt32(/*"HexValue"*/);
String hexRepresentation = Convert.ToString(value, 16);

Comments

0

It sounds like you want this...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.