By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,703 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,703 IT Pros & Developers. It's quick & easy.

Hex with leading zeros to string?

P: n/a
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.
Feb 10 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Rich Raffenetti" <ra********@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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) ?

--

Al Reid


Feb 10 '06 #2

P: n/a
Al, Thanks loads. It seems a little hokey but it works just like it is
documented.

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:uY**************@TK2MSFTNGP15.phx.gbl...
"Rich Raffenetti" <ra********@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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) ?

--

Al Reid


Feb 10 '06 #3

P: n/a
Hi Rich

String.Format("{0:X4}", value)

HTH

Charles
"Rich Raffenetti" <ra********@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Feb 10 '06 #4

P: n/a
"Rich Raffenetti" <ra********@comcast.net> schrieb
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
Feb 10 '06 #5

P: n/a
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.

"Armin Zingler" <az*******@freenet.de> wrote in message
news:uu**************@TK2MSFTNGP11.phx.gbl...
"Rich Raffenetti" <ra********@comcast.net> schrieb
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

Feb 11 '06 #6

P: n/a
I like the String.Format(...) version, myself ;-)

Charles
"Rich Raffenetti" <ra********@comcast.net> wrote in message
news:OJ**************@TK2MSFTNGP14.phx.gbl...
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.

"Armin Zingler" <az*******@freenet.de> wrote in message
news:uu**************@TK2MSFTNGP11.phx.gbl...
"Rich Raffenetti" <ra********@comcast.net> schrieb
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


Feb 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.