|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Format function question.....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 "John Morley" <jmorley@nospamanalysistech.com> wrote in message Right$("0" & Hex$(x), 2)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. 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 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 > > > 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 >> >> 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) -- Show quoteHide quoteRick (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 > > 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 >> >> > "John Morley" <jmorley@nospamanalysistech.com> wrote in message The "00" format specification works ONLY with numbers. Hex(0) through Hex(9) 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) 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. 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 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 -- Show quoteHide quoteRick (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 > > 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. -- Show quoteHide quoteRick (MVP - Excel) "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 >> >> > 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 -- Show quoteHide quoteRick (MVP - Excel) "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 |
|||||||||||||||||||||||