Hex with leading zeros to string?

  • Thread starter Thread starter Al Reid
  • Start date Start date
A

Al Reid

Rich Raffenetti said:
How can one format an integer into a hex string with leading zeros?
Suppose an integer is 512 which in Hex is 200. I wish to print the 4-byte
integer as 0200 or even 0x0200. The HEX function doesn't create leading
zeros. The Format function (format(value,"X") doesn't create leading
zeros.

I believe there should be a simple way that doesn't involving measuring
and padding the string.
How about
Hex(value).PadLeft(4, "0"c) ?
 
How can one format an integer into a hex string with leading zeros? Suppose
an integer is 512 which in Hex is 200. I wish to print the 4-byte integer
as 0200 or even 0x0200. The HEX function doesn't create leading zeros. The
Format function (format(value,"X") doesn't create leading zeros.

I believe there should be a simple way that doesn't involving measuring and
padding the string.
 
Al, Thanks loads. It seems a little hokey but it works just like it is
documented.
 
Rich Raffenetti said:
How can one format an integer into a hex string with leading zeros?
Suppose an integer is 512 which in Hex is 200. I wish to print the
4-byte integer as 0200 or even 0x0200. The HEX function doesn't
create leading zeros. The Format function (format(value,"X")
doesn't create leading zeros.

I believe there should be a simple way that doesn't involving
measuring and padding the string.

Value.ToString("X4")


Armin
 
Thanks to all for the three options. I like Value.ToString("X4") best but I
also like the padleft method for other uses.

Having done mostly VB6, I am new to .Net programming and am interested in
recommended books that would contain these kinds of details.
 
Back
Top