From: RDub on

"Larry Serflaten" <serflaten(a)> wrote in message
> "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
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!


From: Bob Butler on

"RDub" <rweinerattrcrentdotcom> wrote in message
> 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>

From: Jeff Johnson on
"RDub" <rweinerattrcrentdotcom> wrote in message

> 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)

From: Nobody on
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


Debug.Pring GertHex(90)