Home All Groups Group Topic Archive Search About

Format function question.....

Author
28 May 2009 1:25 PM
John Morley
Hi all,

I want to convert an integer value from 0 to 15 into it's hex
representation for display. I want to display a full hex byte so the
display value should have a leading zero, ie. 00 ----> 0F.

If I do this:

Format(Hex(9), "00"), I get '09' as desired

however, if I do this:

Format(Hex(10), "00"), I only get this 'A' (no leading zero)

So, integer values from 0 to 9 work fine, but values from 10 to 15 don't
work as desired. I've played with this a bit without success. Any
suggestions?

John

Author
28 May 2009 1:35 PM
Bob Butler
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
news:%23$5eUf53JHA.1196@TK2MSFTNGP03.phx.gbl...
> Hi all,
>
> I want to convert an integer value from 0 to 15 into it's hex
> representation for display. I want to display a full hex byte so the
> display value should have a leading zero, ie. 00 ----> 0F.

Right$("0" & Hex$(x), 2)
Author
28 May 2009 1:39 PM
Henning
Try
Right$("00" & Hex$(x),2)

/Henning

Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> skrev i meddelandet
news:%23$5eUf53JHA.1196@TK2MSFTNGP03.phx.gbl...
> Hi all,
>
> I want to convert an integer value from 0 to 15 into it's hex
> representation for display. I want to display a full hex byte so the
> display value should have a leading zero, ie. 00 ----> 0F.
>
> If I do this:
>
> Format(Hex(9), "00"), I get '09' as desired
>
> however, if I do this:
>
> Format(Hex(10), "00"), I only get this 'A' (no leading zero)
>
> So, integer values from 0 to 9 work fine, but values from 10 to 15 don't
> work as desired. I've played with this a bit without success. Any
> suggestions?
>
> John
Author
28 May 2009 1:54 PM
John Morley
Thanks guys, works like a charm, and I learned something to boot :-)!

John


Henning wrote:
Show quoteHide quote
> Try
> Right$("00" & Hex$(x),2)
>
> /Henning
>
> "John Morley" <jmorley@nospamanalysistech.com> skrev i meddelandet
> news:%23$5eUf53JHA.1196@TK2MSFTNGP03.phx.gbl...
>
>>Hi all,
>>
>>I want to convert an integer value from 0 to 15 into it's hex
>>representation for display. I want to display a full hex byte so the
>>display value should have a leading zero, ie. 00 ----> 0F.
>>
>>If I do this:
>>
>>Format(Hex(9), "00"), I get '09' as desired
>>
>>however, if I do this:
>>
>>Format(Hex(10), "00"), I only get this 'A' (no leading zero)
>>
>>So, integer values from 0 to 9 work fine, but values from 10 to 15 don't
>>work as desired. I've played with this a bit without success. Any
>>suggestions?
>>
>>John
>
>
>
Author
28 May 2009 2:53 PM
Saga
Yes, that sometimes you have to cheat <bg>!
Saga

Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
news:%23xtwMv53JHA.1512@TK2MSFTNGP05.phx.gbl...
> Thanks guys, works like a charm, and I learned something to boot :-)!
>
> John
>
>
> Henning wrote:
>> Try
>> Right$("00" & Hex$(x),2)
>>
>> /Henning
>>
>> "John Morley" <jmorley@nospamanalysistech.com> skrev i meddelandet
>> news:%23$5eUf53JHA.1196@TK2MSFTNGP03.phx.gbl...
>>
>>>Hi all,
>>>
>>>I want to convert an integer value from 0 to 15 into it's hex
>>>representation for display. I want to display a full hex byte so the
>>>display value should have a leading zero, ie. 00 ----> 0F.
>>>
>>>If I do this:
>>>
>>>Format(Hex(9), "00"), I get '09' as desired
>>>
>>>however, if I do this:
>>>
>>>Format(Hex(10), "00"), I only get this 'A' (no leading zero)
>>>
>>>So, integer values from 0 to 9 work fine, but values from 10 to 15 don't
>>>work as desired. I've played with this a bit without success. Any
>>>suggestions?
>>>
>>>John
>>
>>
Author
29 May 2009 5:05 PM
Rick Rothstein
Since the OP only wants to handle values between 0 and 15, their Hex values
will *always* be single hex-digits, so you can simply concatenate the 0 onto
the front and eliminate the Right function call...

"0" & Hex$(x)

--
Rick (MVP - Excel)


Show quoteHide quote
"Henning" <computer_h***@coldmail.com> wrote in message
news:e9k1Mn53JHA.1712@TK2MSFTNGP03.phx.gbl...
> Try
> Right$("00" & Hex$(x),2)
>
> /Henning
>
> "John Morley" <jmorley@nospamanalysistech.com> skrev i meddelandet
> news:%23$5eUf53JHA.1196@TK2MSFTNGP03.phx.gbl...
>> Hi all,
>>
>> I want to convert an integer value from 0 to 15 into it's hex
>> representation for display. I want to display a full hex byte so the
>> display value should have a leading zero, ie. 00 ----> 0F.
>>
>> If I do this:
>>
>> Format(Hex(9), "00"), I get '09' as desired
>>
>> however, if I do this:
>>
>> Format(Hex(10), "00"), I only get this 'A' (no leading zero)
>>
>> So, integer values from 0 to 9 work fine, but values from 10 to 15 don't
>> work as desired. I've played with this a bit without success. Any
>> suggestions?
>>
>> John
>
>
Author
29 May 2009 8:10 PM
Henning
Just a bad/good habit I have, hard to let go of the fingers typing that way
;)

/Henning

Show quoteHide quote
"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> skrev i meddelandet
news:uDWt6%23H4JHA.6004@TK2MSFTNGP02.phx.gbl...
> Since the OP only wants to handle values between 0 and 15, their Hex
> values will *always* be single hex-digits, so you can simply concatenate
> the 0 onto the front and eliminate the Right function call...
>
> "0" & Hex$(x)
>
> --
> Rick (MVP - Excel)
>
>
> "Henning" <computer_h***@coldmail.com> wrote in message
> news:e9k1Mn53JHA.1712@TK2MSFTNGP03.phx.gbl...
>> Try
>> Right$("00" & Hex$(x),2)
>>
>> /Henning
>>
>> "John Morley" <jmorley@nospamanalysistech.com> skrev i meddelandet
>> news:%23$5eUf53JHA.1196@TK2MSFTNGP03.phx.gbl...
>>> Hi all,
>>>
>>> I want to convert an integer value from 0 to 15 into it's hex
>>> representation for display. I want to display a full hex byte so the
>>> display value should have a leading zero, ie. 00 ----> 0F.
>>>
>>> If I do this:
>>>
>>> Format(Hex(9), "00"), I get '09' as desired
>>>
>>> however, if I do this:
>>>
>>> Format(Hex(10), "00"), I only get this 'A' (no leading zero)
>>>
>>> So, integer values from 0 to 9 work fine, but values from 10 to 15 don't
>>> work as desired. I've played with this a bit without success. Any
>>> suggestions?
>>>
>>> John
>>
>>
>
Author
28 May 2009 3:36 PM
Jeff Johnson
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
news:%23$5eUf53JHA.1196@TK2MSFTNGP03.phx.gbl...

> If I do this:
>
> Format(Hex(9), "00"), I get '09' as desired
>
> however, if I do this:
>
> Format(Hex(10), "00"), I only get this 'A' (no leading zero)

The "00" format specification works ONLY with numbers. Hex(0) through Hex(9)
return strings that can be interpreted as numbers, and therefore Format()
applies the correct...formatting. 10-15 return A-F, which Format() cannot
interpret as a number, and therefore it ignores the format string and just
returns the input.
Author
28 May 2009 5:39 PM
Nobody
Here is my version in the form of a function, which can support up to 8 hex
digits(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
Author
29 May 2009 5:17 PM
Rick Rothstein
While I would also use the Right function method you have outlined, I just
wanted to demonstrate that here is a Format function call that could also be
used...

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

--
Rick (MVP - Excel)


Show quoteHide quote
"Nobody" <nob***@nobody.com> wrote in message
news:u$X56s73JHA.480@TK2MSFTNGP06.phx.gbl...
> Here is my version in the form of a function, which can support up to 8
> hex digits(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
>
>
Author
29 May 2009 5:36 PM
Rick Rothstein
By the way, you can remove the 8 hex digit limit by substituting a String
function call for the 8 zeroes...

Public Function GetHex(ByVal i As Long, _
   Optional ByVal HexWidth As Integer = 2) As String
   GetHex = Format(String(HexWidth, "0") & Hex$(i), _
                      "!" & String(HexWidth, "@"))
End Function

Note that you can do the same thing for your originally posted function as
well...

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

I'm guessing you might want to throw some error checking in either of these
in order to catch cases when HexWidth is not long enough to accommodate the
hex value.

--
Rick (MVP - Excel)


Show quoteHide quote
"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
news:OjzddFI4JHA.3304@TK2MSFTNGP06.phx.gbl...
> While I would also use the Right function method you have outlined, I just
> wanted to demonstrate that here is a Format function call that could also
> be used...
>
> Public Function GetHex(ByVal i As Long, _
>    Optional ByVal HexWidth As Integer = 2) As String
>    GetHex = Format("00000000" & Hex$(i), "!" & String(HexWidth, "@"))
> End Function
>
> --
> Rick (MVP - Excel)
>
>
> "Nobody" <nob***@nobody.com> wrote in message
> news:u$X56s73JHA.480@TK2MSFTNGP06.phx.gbl...
>> Here is my version in the form of a function, which can support up to 8
>> hex digits(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
>>
>>
>
Author
29 May 2009 5:38 PM
Rick Rothstein
Here is another approach the OP can try...

Public Function GetHex(ByVal i As Long, _
    Optional ByVal HexWidth As Integer = 2) As String
    Dim HexVal As String
    HexVal = Hex$(i)
    If Len(HexVal) <= HexWidth Then
      GetHex = String$(HexWidth, "0")
      Mid$(GetHex, HexWidth - Len(HexVal) + 1) = HexVal
    Else
      '
      ' Handle "Too Many Hex Digits for Specified HexWidth" error here
      '
    End If
End Function

--
Rick (MVP - Excel)


Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
news:%23$5eUf53JHA.1196@TK2MSFTNGP03.phx.gbl...
> Hi all,
>
> I want to convert an integer value from 0 to 15 into it's hex
> representation for display. I want to display a full hex byte so the
> display value should have a leading zero, ie. 00 ----> 0F.
>
> If I do this:
>
> Format(Hex(9), "00"), I get '09' as desired
>
> however, if I do this:
>
> Format(Hex(10), "00"), I only get this 'A' (no leading zero)
>
> So, integer values from 0 to 9 work fine, but values from 10 to 15 don't
> work as desired. I've played with this a bit without success. Any
> suggestions?
>
> John