|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Formating Hex ValuesI 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
Show quote
Hide quote
"RDub" <rweinerattrcrentdotcom> wrote in message Hex(90) = "5A" which VB is coercing to 5:00AM and then trying to apply the 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? "00" formatting to that. ?Format$("5A","hh") 05
Show quote
Hide quote
"Bob Butler" <bob_butler@cox.invalid> wrote in message That was driving me crazy. I never would have guessed that VB would think 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! 5A was a time value. Sheesh.. Note to self... Be wary of Format()! Rdub "RDub" <rweinerattrcrentdotcom> wrote in message Not just format but any place VB attempts to coerce from string to numeric. 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()! 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> RDub expressed precisely :
> "Bob Butler" <bob_butler@cox.invalid> wrote: Be wary of ETC! Do your own coercions. Don't ever let VB guess. :-)>> 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()!
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 > > "RDub" <rweinerattrcrentdotcom> wrote Try this:> 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? for i = 89 to 91 : ? i, format("&H" & hex(i),"00") : next LFS
Show quote
Hide quote
"Larry Serflaten" <serfla***@gmail.com> wrote in message Yup, that would work just fine too. I just never saw to potential for VB tonews: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 > think that the resultant number might (Ought to) be a time. Lesson learned! Rdub "RDub" <rweinerattrcrentdotcom> wrote in message There's no need for two zeroes. Hex() will always return at least one news:OVzFLclOLHA.1172@TK2MSFTNGP04.phx.gbl... > right("00" & hex(SomeByte),2) character, so you only need Right$("0" & Hex$(SomeByte), 2) 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) |
|||||||||||||||||||||||