views:

45

answers:

2

Hi, i need to convert integer number to hex value that will look like this:

0x0201cb77192c851c

When i do in C#

string hex = int.ToString("x")

it returns me this: 201cb77192c851c

How can i achieve needed result?

+4  A: 

One way would be to append the number of digits you need, after "x". This will pad the output with leading zeros as necessary.

"0x" + myLong.ToString("x16");

or

string.Format("0x{0:x16}", myLong);

From The Hexadecimal ("X") Format Specifier :

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.

Ani
well, your answer works, if i upvote yours will you upvote mine lol
John Boker
upvoted, it works.
John Boker
@John Boker: Thanks, but yours needs the precision specifier.
Ani
@ani the precision specifier wasnt in the specs.
John Boker
@John Boker: Note the leading zero in the OP's sample.
Ani
@ani, you are correct, im drunk, disregard what i say... i want 13k, almost there. :)
John Boker
@John Boker: lol. Just correct your answer and I will do the needful.
Ani
@ani i added the 16 to my x. i shouldnt drink on a work nigth.
John Boker
thanks @ani! more characters.
John Boker
+2  A: 
string hex = "0x" + int.ToString("x16")
John Boker