Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to convert an integer number to the hex value. It will look like this:

0x0201cb77192c851c

When I do

string hex = int.ToString("x")

in C#, it returns

201cb77192c851c

How can I get the required result?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
251 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...