Home All Groups Group Topic Archive Search About

Hex with leading zeros to string?

Author
10 Feb 2006 5:33 AM
Rich Raffenetti
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.

Author
10 Feb 2006 1:02 AM
Al Reid
"Rich Raffenetti" <raffene***@comcast.net> wrote in message
news:%23LJyordLGHA.1424@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
Author
10 Feb 2006 7:21 AM
Rich Raffenetti
Al,  Thanks loads.  It seems a little hokey but it works just like it is
documented.

Show quoteHide quote
"Al Reid" <arei***@reidDASHhome.com> wrote in message
news:uYXxj2dLGHA.2628@TK2MSFTNGP15.phx.gbl...
> "Rich Raffenetti" <raffene***@comcast.net> wrote in message
> news:%23LJyordLGHA.1424@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
>
>
>
>
>
>
Author
10 Feb 2006 10:52 AM
Charles Law
Hi Rich

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

HTH

Charles


Show quoteHide quote
"Rich Raffenetti" <raffene***@comcast.net> wrote in message
news:%23LJyordLGHA.1424@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.
>
Author
10 Feb 2006 3:20 PM
Armin Zingler
"Rich Raffenetti" <raffene***@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
Author
11 Feb 2006 6:12 AM
Rich Raffenetti
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.

Show quoteHide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:uuZn0YlLGHA.3260@TK2MSFTNGP11.phx.gbl...
> "Rich Raffenetti" <raffene***@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
Author
11 Feb 2006 4:01 PM
Charles Law
I like the String.Format(...) version, myself ;-)

Charles


Show quoteHide quote
"Rich Raffenetti" <raffene***@comcast.net> wrote in message
news:OJIwWmqLGHA.2696@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.nospam@freenet.de> wrote in message
> news:uuZn0YlLGHA.3260@TK2MSFTNGP11.phx.gbl...
>> "Rich Raffenetti" <raffene***@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
>
>