Home All Groups Group Topic Archive Search About
Author
12 Aug 2010 7:46 PM
RDub
Guys/Gals

I need to display byte values in Hex format and preserve leading zeros. so I
came up with :
format(hex(SomeByte),"00")
to perform the deed.  I just noticed an inconsistency with that code and
changed it to :
right("00" & hex(SomeByte),2)
which fixes my problem.

But I do not understand why the first form gives an incorrect result.  I
just hate not knowing!  Can someone please help out?

from the immediate window try this:
for i = 89 to 91 : ? i, format(hex(i),"00") : next
89           59
90           00        <--- What's up with this?
91           5B

and then this:
for i = 89 to 91 : ? i, right("00" & hex(i),2) : next
89           59
90           5A      <--- Correct
91           5B

Rdub

Author
12 Aug 2010 8:01 PM
Bob Butler
Show quote Hide quote
"RDub" <rweinerattrcrentdotcom> wrote in message
news:OVzFLclOLHA.1172@TK2MSFTNGP04.phx.gbl...
> Guys/Gals
>
> I need to display byte values in Hex format and preserve leading zeros. so
> I came up with :
> format(hex(SomeByte),"00")
> to perform the deed.  I just noticed an inconsistency with that code and
> changed it to :
> right("00" & hex(SomeByte),2)
> which fixes my problem.
>
> But I do not understand why the first form gives an incorrect result.  I
> just hate not knowing!  Can someone please help out?
>
> from the immediate window try this:
> for i = 89 to 91 : ? i, format(hex(i),"00") : next
> 89           59
> 90           00        <--- What's up with this?

Hex(90) = "5A" which VB is coercing to 5:00AM and then trying to apply the
"00" formatting to that.

?Format$("5A","hh")
05
Author
12 Aug 2010 8:19 PM
RDub
Show quote Hide quote
"Bob Butler" <bob_butler@cox.invalid> wrote in message
news:i41k1n$vm2$1@news.eternal-september.org...
>
> "RDub" <rweinerattrcrentdotcom> wrote in message
> news:OVzFLclOLHA.1172@TK2MSFTNGP04.phx.gbl...
>> Guys/Gals
>>
>> I need to display byte values in Hex format and preserve leading zeros.
>> so I came up with :
>> format(hex(SomeByte),"00")
>> to perform the deed.  I just noticed an inconsistency with that code and
>> changed it to :
>> right("00" & hex(SomeByte),2)
>> which fixes my problem.
>>
>> But I do not understand why the first form gives an incorrect result.  I
>> just hate not knowing!  Can someone please help out?
>>
>> from the immediate window try this:
>> for i = 89 to 91 : ? i, format(hex(i),"00") : next
>> 89           59
>> 90           00        <--- What's up with this?
>
> Hex(90) = "5A" which VB is coercing to 5:00AM and then trying to apply the
> "00" formatting to that.
>
> ?Format$("5A","hh")
> 05
>
Thanks Bob!

That was driving me crazy.  I never would have guessed that VB would think
5A was a time value.  Sheesh.. Note to self... Be wary of Format()!

Rdub
Author
12 Aug 2010 8:49 PM
Bob Butler
"RDub" <rweinerattrcrentdotcom> wrote in message
news:Ozm46ulOLHA.5064@TK2MSFTNGP02.phx.gbl...
> That was driving me crazy.  I never would have guessed that VB would think
> 5A was a time value.  Sheesh.. Note to self... Be wary of Format()!

Not just format but any place VB attempts to coerce from string to numeric.
Your initial version did numeric to string with the Hex function, then
string to numeric as the first parameter to the format function, then
numeric to string again as the result of the format.  That's just begging
for VB to apply a little ETC somewhere along the line. <g>
Author
7 Sep 2010 10:45 PM
Karl E. Peterson
RDub expressed precisely :
> "Bob Butler" <bob_butler@cox.invalid> wrote:
>> Hex(90) = "5A" which VB is coercing to 5:00AM and then trying to apply the
>> "00" formatting to that.
>>
>> ?Format$("5A","hh")
>> 05
>>
> Thanks Bob!
>
> That was driving me crazy.  I never would have guessed that VB would think 5A
> was a time value.  Sheesh.. Note to self... Be wary of Format()!

Be wary of ETC!  Do your own coercions.  Don't ever let VB guess.  :-)

--
..NET: It's About Trust!
http://vfred.mvps.org
Author
12 Aug 2010 8:12 PM
Larry Serflaten
Show quote Hide quote
"RDub" <rweinerattrcrentdotcom> wrote in message news:OVzFLclOLHA.1172@TK2MSFTNGP04.phx.gbl...
> Guys/Gals
>
> I need to display byte values in Hex format and preserve leading zeros. so I
> came up with :
> format(hex(SomeByte),"00")
> to perform the deed.  I just noticed an inconsistency with that code and
> changed it to :
> right("00" & hex(SomeByte),2)
> which fixes my problem.
>
> But I do not understand why the first form gives an incorrect result.  I
> just hate not knowing!  Can someone please help out?
>
> from the immediate window try this:
> for i = 89 to 91 : ? i, format(hex(i),"00") : next
>  89           59
>  90           00        <--- What's up with this?
>  91           5B
>
> and then this:
> for i = 89 to 91 : ? i, right("00" & hex(i),2) : next
>  89           59
>  90           5A      <--- Correct
>  91           5B
>
> Rdub
>
>
Author
12 Aug 2010 8:13 PM
Larry Serflaten
"RDub" <rweinerattrcrentdotcom> wrote

> I need to display byte values in Hex format and preserve leading zeros. so I
> came up with :
> format(hex(SomeByte),"00")
> to perform the deed.  I just noticed an inconsistency with that code and
> changed it to :
> right("00" & hex(SomeByte),2)
> which fixes my problem.
>
> But I do not understand why the first form gives an incorrect result.  I
> just hate not knowing!  Can someone please help out?

Try this:

for i = 89 to 91 : ? i, format("&H" & hex(i),"00") : next


LFS
Author
12 Aug 2010 8:32 PM
RDub
Show quote Hide quote
"Larry Serflaten" <serfla***@gmail.com> wrote in message
news:i41kjl$5m3$1@news.eternal-september.org...
>
> "RDub" <rweinerattrcrentdotcom> wrote
>
>> I need to display byte values in Hex format and preserve leading zeros.
>> so I
>> came up with :
>> format(hex(SomeByte),"00")
>> to perform the deed.  I just noticed an inconsistency with that code and
>> changed it to :
>> right("00" & hex(SomeByte),2)
>> which fixes my problem.
>>
>> But I do not understand why the first form gives an incorrect result.  I
>> just hate not knowing!  Can someone please help out?
>
> Try this:
>
> for i = 89 to 91 : ? i, format("&H" & hex(i),"00") : next
>
>
> LFS
>
Yup, that would work just fine too.  I just never saw to potential for VB to
think that the resultant number might (Ought to) be a time.

Lesson learned!

Rdub
Author
12 Aug 2010 9:07 PM
Jeff Johnson
"RDub" <rweinerattrcrentdotcom> wrote in message
news:OVzFLclOLHA.1172@TK2MSFTNGP04.phx.gbl...

> right("00" & hex(SomeByte),2)

There's no need for two zeroes. Hex() will always return at least one
character, so you only need

    Right$("0" & Hex$(SomeByte), 2)
Author
12 Aug 2010 10:13 PM
Nobody
For any length up to 32 Bits:

Public Function GetHex(ByVal i As Long, _
    Optional ByVal HexWidth As Integer = 2) As String
    GetHex = Right$("00000000" & Hex$(i), HexWidth)
End Function

Usage:

Debug.Pring GertHex(90)