Formating Hex Values

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 


0
RDub
8/12/2010 7:46:20 PM
vb.general.discussion 1016 articles. 0 followers. Follow

9 Replies
1122 Views

Similar Articles

[PageSpeed] 23

"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


0
Bob
8/12/2010 8:01:17 PM
"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
>
>


0
Larry
8/12/2010 8:12:09 PM
"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


0
Larry
8/12/2010 8:13:20 PM
"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 


0
RDub
8/12/2010 8:19:53 PM
"Larry Serflaten" <serflaten@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


0
RDub
8/12/2010 8:32:18 PM
"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>



0
Bob
8/12/2010 8:49:35 PM
"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) 


0
Jeff
8/12/2010 9:07:01 PM
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)


0
Nobody
8/12/2010 10:13:12 PM
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


0
Karl
9/7/2010 10:45:37 PM
Reply:

Similar Artilces:

Sumif returning incorrect value
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel I have data in columns c to f and rows 2 to 20 <br> I also have data in a2 to a20. <br> I am trying to sum the data in s2 to f20 when the row matches my criteria in a2 to a 20. The formula that I am using is <br><br>sumif(a2:a20,&quot;=12&quot;,c2:f20) <br><br>the result will only sum the values in column c. <br><br>Does anyone know what the problem is? <br><br>Thanks <br><br>Allan SUMIF() has various limitations. this will work,...

Custom formatting #7
How can I do the following custom formatting – I need trailing dots before the number ……..1,234.00 Thanks for any help Right Click > Format Cells > Select Custom Enter "........ "0.00 "John Knoke" <JohnKnoke@discussions.microsoft.com> wrote in message news:586761E6-32A9-4911-BF50-3790614145B2@microsoft.com... > > How can I do the following custom formatting - > I need trailing dots before the number > ....1,234.00 > > Thanks for any help > > John, Use a custom number format of *.#,###.00 Note the period between the * and the f...

Convert time stored as decimalised number to time format
Hello How can I convert 3.5 hours to 3:30:00? -- Emma Emma A1/24 and formate as time Mike Rogers "Emma" wrote: > Hello > How can I convert 3.5 hours to 3:30:00? > -- > Emma ...

Keeping the format but update the data
I have a report I run everyday. I want to update the data, but keep the column sizes and formatting. Any suggestions? Anybody suggest a good ebook to become more formuliar with excel? JoeM JoeM, this could be as simple as recording a macro during the formatting of the sheet after importing the data once. Then each day, after importing the new data, you simply run that macro. As for online eBook's for Excel, I'm not actually aware of any although I suspect a Google search would turn up several. It's hard to beat the "For Dummies..." series of hardcopy...

NEED HELP-wrong formatting saved ??
After using word 2007 and saving a word file, it changed the formatting from the previous file (i think it was .wps microsoft works word processor) and now its all messed up.....I think I saved it in some other wrong encoding standard......now, whenever I open this file now half of it is in some weird looking unreadable characters....like this with wha捬屨捦ㅳ尠晡‰汜牴档晜獣‰歜牥楮杮尰扤档慜㍦㔱㔰楜獮獲摩㌱ㄳ〶‷਍灜牡素筽⩜灜獮捥癬ㅬ灜畮牣屭湰瑳牡ㅴ灜楮摮湥㝴〲灜桮湡⁧屻湰硴慴ges! That is what seems to happen once the system breaks its laws from the inside. ......couldnt find online solutions...does anyone have any suggestions or s...

Conditional Formating: linking to display another cell
Hello, I am trying to make a traffic light with symbols and I've read to 'use a separate cell for the dropdown choices, with their resulting value linked into the formatted cell through an IF function, using the character that you want to display.' So: =IF someone enters '1' in B8, THEN display contents of $C$4 (will it display font and attributes?) =IF someone enters '2' in B8, THEN display contents of $C$5 =IF someone enters '3' in B8, THEN display contents of $C$6 But I've been reading everywhere and CF is very new to me and I need ...

Copying values from place to place
Hello community: Is it possible say, to type a value in a textbox in one form (Form A), automatically copy that value, then open another form( Form B) and automatically paste that value into another textbox in Form B? Is this possible? Thank you for your help. -Gabriel M. If you open Form B from Form A you can use the OpenArgs of the OpenForm command line to pass a value to Form B. docmd.OpenForm "FormName",,,,,,Me.TextBoxName On the OnLoad event of the Form B you can use set the value in the text box If not IsNull(Me.OpenArgs) Then Me.TextBox = Me.OpenArgs End If -- ...

Infopath w/ manually entered values in drop-down and qry results
I had originally posted this elsewhere, but was told this forum is the appropariate plase. I have an Infopath form with a drop-down listbox, that is poulated with manually-entered values. I choose a value, submit the updated data, and it does put the correct value in the SQL 2005 database. However, the next time I query the data, the value in the drop-down list box is the default value for the list-box, not the value from the database, which is misleading. I would have expected the drop-down listbox to display the value from the database instead. Thanks. On Tue, 4 Sep 2007, in...

Newbie Needs Help With Formatting E-Mails
Newbie Needs Help With Formatting E-Mails Hi all. I'm new to this site and would appreciate some formatting help. I created a new Word doc and copied and pasted some graphics and some simple text boxes in to it. The graphics and text boxes were created in Powerpoint by someone else. Looks fine on my screen. Before sending it out via e-mail to an Outlook e-mail group I have, I first sent myself a test message to see what it would look like on the receiving end. It looks a mess! The text boxes are all over the place, as is the text that is not in a box. Also, everything slide over to ...

2003 format in 2007?
I'm used to Outlook 2003, but I just bought a new computer and installed Outlook 2007. I view a week at a time, and I prefer the calendar format in 2003 -- it shows the whole week in two columns and I didn't have to scroll. Is there a way I can use the same format in 2007? No, unfortunately that view had to be removed to enabled calendar overlay. Try a two week view: http://slipstick.me/2wk -- Diane Poremsky [MVP - Outlook] Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com/ Outlook Tips by email: mailto:dai...

Average & eliminating zero value Need HELP !! PLEASE
In cell C4 im calculating average hours for D5:D65 of all employees In cells Z5:Z65 If L is entered means laid off In cells AB5:AB65 IS AVERAGE OF ALL EMPOLYEES HOURS in cells D5:D65 if L is entered in Z Column In cells D5:D65 IS all employees hours FORMULA FOR C4 IS {=AVERAGE(IF(ISNUMBER(D5:D65),IF(D5:D65>0,D5:D65,0)))} Formula for cells AB5:AB65 IS =IF(Z5="L",R4,"") Formula for D5:D65 IS =IF(Z5>0,0,IF(B5="","",AA5+SUM(E5:J5)*1.5+K5*2)) I want to average all employees hours except if L is entered in Z Column so say 65 empl...

formatting #2
Is the strikethrough formatting option no longer available in Publisher? Or if you need to use a strikethrough do you have to draw a line through the text? The line does not always stay in the same spot when you group and then have to paste on another page. Waggie wrote: > Is the strikethrough formatting option no longer available in > Publisher? Or if you need to use a strikethrough do you have to draw > a line through the text? The line does not always stay in the same > spot when you group and then have to paste on another page. ========================== You have the an...

Logical Filtering based on Value
I've been using this formula to sort some values and return results if( or( and( b9<>"", isnumber( find( Left(b9,1 ,"AWFUY") ) ), isnumber( find("SHORT",g9 ) ), sum( countif(c9,{"BOG","BLM","CMO"}) ) ) ) ) ,i9-0.01,j9 ) --I used ALT-ENTER between each formula so as to see thing clearly Column A values can be "B01" Column B values can be "BRN" Columns I & J are values, one being lowest the other being highest allowed. On another sheet I...

Text formatted email
I am trying to email a long hyperlink path to some one that can only receive text formatted emails. When they receive the email it, the hyperlink gets converted in to 2 lines, top line is a hyperlink (path incomplete) the second line is "text" with the balance of the hyperlink path. I placed < > around the hyperlink and it still splits in to 2 lines. Is there any way to keep the full link path intact? Thanks! jvs Tiny URL to the rescue! :-) http://tinyurl.com/ -- Robert Sparnaaij [MVP-Outlook] www.howto-outlook.com Tips of the month: -What do the Outlook Icons Mean? -Cr...

Find a value in cells
Hi, all, I have something like this : a 1 b 4 c 5 d 3 I'm doing a max function in the number column and it returns me '5' But, after that, I want to know the letter which corresponds to the max number How can I do that? Thanks Nic -- nicgendron ------------------------------------------------------------------------ nicgendron's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=25151 View this thread: http://www.excelforum.com/showthread.php?threadid=386417 Let's say that...

Trying to get a type of video file format to open and run on my we
I created a video using Windows Movie maker and saved as .wmp file. I saved it to my computer and then inserted on my web page under Front Page 2003. Nothing! I then converted the file to avi format - still nothing! I then tried mpeg4 still nothing. The manual is next to useless on this subject. Anyone tell me what I need to do so that when a user opens up the page the video plays automatically. -- Simon Holloway Save as a .wmv and insert a suitable player into the page. For IE only, Insert->Web Component->Advanced Controls->ActiveX Control Click Next Choose Window...

Import data not in correct format
Import data reads: Need to do statistical anaysis on hundreds of numbers and Cell value # of Cells don't want to enter the numbers by hand. 3 1 4 2 Would like: Cell Value 3 4 4 ...

How to force the user to fill in values for yes/no fields ?
Hi ! I'm using MS Access 2003. I have a table with a number of yes/no fields. When a new record is started, I want the user to explicitly specify a value for each of these fields, e.g. by choosing one of two radio buttons associated with the field. Now, it was no problem for me to arrange an option group with two suitable radio buttons for each of these fields. The user can then choose explicitly between yes and no. However, all fields seem to have implicit values immediately after a new record has been started. Therefore, the new record can be stored in the db without the user making...

Four Conditional Formats?
Dear all, I am attempting to set up a template for someone else to use on another machine, and wish conditional format a cell such that it has four potential formats. The first is based on an adjacent cell value, and if this is met, then it is colour 1. The remaining three, are all AND statements involving the first cell and another cell value being less than date A, less than date B or less than date C. Is this possible to do? I have limited VB knowledge, and am wary of using add-ins as the people I am sending this to to use have limited excel knowledge. Regards, Steve -- stevepain -...

finding values in a sum
if you have an array of numbers and want to identify which of those numbers add up to a specified value, is there a function in excel that can help you to find the correct combination of numbers. eg in a simple example; if the array of numbers was 2,3,5,6 and the specified value was 9, we know the only combination of numbers from this array that would sum to give the value 9 are 6 & 3, however with a larger array of numbers (20) or more, it would be more difficult solve the problem manually. http://groups.google.com/groups?threadm=e3iWLUiYDHA.2960%40tk2msftngp13.phx. gbl See my previo...

Conditional Format
Hi, I want to assign a number to a certain conditional format. When the condition is true it should put a 1 in the cell and when the condition is false a zero or nothing should be assigned. Anyone? I seem to make the wrong code all the time. Thanks, Johanna Use the same test that the CF uses, and return 1 for TRUE and "" for FALSE. -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "Jootje" <Jootje@discussions.microsoft.com> wrote in message news:CB0B3201-2DD9-466A-94FF-108691C77FAF@microsoft.com... > Hi, > >...

my formats get pushed out when I insert columns
Hi folks, I am having trouble when inserting columns in a large data field. Whenever I insert a column at some point in the data field, th formulas within the data field adjust, or 'stretch', to accomodate th added column, however my conditional formatting gets pushed along t the right and doesn't adapt itself in the same way the formulas do. This means whenever I insert a column, the formulas remain correct bu I have to reset the conditional formatting for the entire data field. I would be extremely grateful for any advice on this situation. Best regards, melvi -- Message pos...

Value based Purchase Order
Hi, We have a customer in the service industry using GP Ver 9.0. GP does not seem to be addressing few key requirements for the service industry and hence request your advice. 1. The Customer requires entering the purchase orders with the value and without any quantity. This is basically for the service type purchase orders. They would like to partially receive and invoice based on the value and not based on the quantity. As a standard functionality, the purchase order screen in GP in the Purchasing module will not allow to enter the value (Extended Cost) without the Quantity colum...

Set a value on a form
I use the following code to open a form DoCmd.OpenForm stDocName, , , stLinkCriteria Is there any way to pass data to the form (through the constructor) or to set the value of a label on the form Thank you, Samuel You can use the OpenArgs argument to pass a value to the new form, and then use the new form's Load event to read the value and put it into a control on itself. First step -- pass the value: DoCmd.OpenForm stDocName, , , stLinkCriteria, , "ValueToPass" Second step - read the value and use it; this code goes with the Load event of the stDocName form: ...

removing duplicate values from a column
It sounds so simple that I must be overlooking the obvious answer, but I can't seem to find a way to remove duplicate values from a column. The case is simple: I have 600+ emailaddresses in column A, sorted alphabetically and there's a bunch of duplicate addresses in there (some of 'em occur up to 7 times!!). Instead of manually running through the whole list, removing the duplicates, there MUST be some soft of filter/function in excel to do this for me... Could anyone PLEASE help me with this!?!? Cheers! -- NOTE: remove the spamtrap from the emailaddress Jops, Select the em...