5

I have an integer that I need to convert to a four digit hex value.

For example, lets say the int value is 16. What I am looking for is a way to go from 16 to 0x00 0x10.

Any suggestions would be greatly appreciated!!!

Jan Tacci
  • 3,131
  • 16
  • 63
  • 83
  • http://stackoverflow.com/questions/4690480/int-to-hex-string – Gir Aug 11 '12 at 14:53
  • Do you mean you want the hex-string representation of the int? – brianestey Aug 11 '12 at 14:53
  • All I know is this.... given an integer 16 I need to produce 2 bytes. The first bytes value is 0x00 and the second bytes value is 0x10. This ultimately ends up as 0000 0000 0001 0000 which = 16. My program need to chop the result up. In other words, take the first 8 bits and display it as 0x00 then take the last 8 bits and display it as 0x10. In summary, I need to go from 16 -> 0x00 0x10. – Jan Tacci Aug 11 '12 at 14:58
  • An int is 32 bits, so you need 4 bytes (8 hex digits) to represent all possible values... – Thomas Levesque Aug 11 '12 at 15:44

4 Answers4

3

Try this:

var input = 16;

var bytes = new byte[2];
bytes[0] = (byte)(input >> 8);  // 0x00
bytes[1] = (byte)input;         // 0x10

var result = (bytes[0] << 8)
           | bytes[1];

// result == 16
dtb
  • 213,145
  • 36
  • 401
  • 431
1

Here's one with regular expressions, just for fun:

Regex.Replace(number.ToString("X4"), "..", "0x$0 ").TrimEnd();
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • If I do that alone, the second byte does not get the prefix. But yeah, you can probably craft a more complex regex that takes care of that too. – Diego Mijelshon Aug 11 '12 at 15:13
0

Shift it! Mask it! Mask it! string.Format it!

int n = 16;
string.Format("0x{0:x2} 0x{1:x2}", (n & 0xff00) >> 8, n & 0xff); // 0x00 0x10

Here's a demo.

The x2 format specifier means a 2-digit hexadecimal value.


Okay, apparently you just want two bytes. Hexadecimal is not relevant here.

byte lowByte = (byte)(n & 0xff);
byte highByte = (byte)(n >> 8 & 0xff);
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

Alternately, a little more general solution is to do it by byte array (then you can use this for strings or other data types)

public static string ByteArrayToString(byte[] ba)
{
   string hex = BitConverter.ToString(ba);
   return hex.Replace("-","");
}

int i = 39;
string str = "ssifm";
long l = 93823;

string hexi = ByteArrayToString(BitConverter.GetBytes(i));
string hexstr = ByteArrayToString(Encoding.Ascii.GetBytes(str));
string hexl = ByteArrayToString(BitConverter.GetBytes(l));

This returns them in a 'FF' format, you can add the '0x' yourself by adding this after the ToString() instead:

return "0x"+hex.Replace("-", " 0x");
K893824
  • 1,279
  • 8
  • 21
  • I am not going for a string... what I have is an array. There are 2 locations in the array where I need to store the value 16. The first position of the array need 0x00 and the second needs 0x01. Note that these are not strings whats being stored in the array are actually bytes so you can also think of it as array[0]=0 and array[1]=10 – Jan Tacci Aug 11 '12 at 15:06
  • @TackyTacky: If you need an array, `BitConverter` (or shifting if it's only the two you need) is more appropriate and hexadecimal is irrelevant. – Ry- Aug 11 '12 at 15:06