|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Hex--->Dec!VB6 has in-built functions to convert numbers from decimal number
system to hexadecimal & octal number system (Hex & Oct respectively) but how do we go the other way round i.e. how to convert numbers from hexadecimal & octal number system to decimal number system? Thanks, Arpan Arpan wrote:
> You mean something like> VB6 has in-built functions to convert numbers from decimal number > system to hexadecimal & octal number system (Hex & Oct respectively) > but how do we go the other way round i.e. how to convert numbers from > hexadecimal & octal number system to decimal number system? > ?val("&H" & hex$(10)) 10 Nope....Duane....that isn't what I am looking out for. Let me give you
an example. Consider the decimal number 10. The hexadecimal equivalent of 10 is 'A' (without the quotes) which can be computed using Hex(10). Now I want a function that will convert the hexadecimal 'A' back to decimal 10. Similarly, the octal equivalent of 10 is 12 which can be computed using Oct(10). Now I want a function that will convert the octal 12 back to decimal 10. I hope I am clear enough now. If still unclear, use the standard Windows calculator in the scientific mode. That will definitely give you a better idea of what I am exactly looking out for. Any further suggestions are most welcome. Thanks, Regards, Arpan Arpan wrote:
> That's <exactly> what I demonstrated...> Nope....Duane....that isn't what I am looking out for. Let me give you > an example. > > Consider the decimal number 10. The hexadecimal equivalent of 10 is 'A' > (without the quotes) which can be computed using Hex(10). Now I want a > function that will convert the hexadecimal 'A' back to decimal 10. Go through my example... ?hex$(10) A ?"&H" & hex$(10) &HA So, "&H" & hex$(10), the argument to Val() is "&HA". The &H is needed to satisfy VB's syntax rules to identify the string "A" to Val() as being interpreted as a hex value. ?val("&H" & hex$(10)) 10 > Similarly, the octal equivalent of 10 is 12 which can be computed using Same thing...> Oct(10). Now I want a function that will convert the octal 12 back to > decimal 10. > I hope I am clear enough now. ... You're confusing internal and external representations, methinks...The decimal value is stored internally as a bit pattern of 000010010 (upper bits implied and all 0). Whether you see "10" or "A" or "12" is simply what base system is used for presentation--the internal representation is unchanged. As noted, there are syntax rules in VB regarding what you have to do to present ASCII representations to various functions/statements to make the interpretation and presentation as desired. If you would describe the actual application/problem domain specific solutions might be forthcoming. Duane Bozarth wrote:
> More precisely, .... > The decimal value is stored internally as a bit pattern of 000010010 should have been "The decimal value 10 is ..."More precisely the binary representation of the decimal value 10 is
'0000 1010' or A in hex you added a zero bit in the middle and what you actually had was the binary representation of 18. *** Sent via Developersdex http://www.developersdex.com *** Scott Beckstead wrote:
> Yep, you're right...a typo maddeningly slipped in....> More precisely the binary representation of the decimal value 10 is > '0000 1010' or A in hex you added a zero bit in the middle and what you > actually had was the binary representation of 18. Duane Bozarth wrote:
> And I'm blaming the bifocals... :)> Scott Beckstead wrote: > > > > More precisely the binary representation of the decimal value 10 is > > '0000 1010' or A in hex you added a zero bit in the middle and what you > > actually had was the binary representation of 18. > > Yep, you're right...a typo maddeningly slipped in.... "Arpan" <arpan***@hotmail.com> wrote in message The routines you are talking about concern the 'presentation' and thenews:1129819855.392370.56630@g47g2000cwa.googlegroups.com... > VB6 has in-built functions to convert numbers from decimal number > system to hexadecimal & octal number system (Hex & Oct respectively) > but how do we go the other way round i.e. how to convert numbers from > hexadecimal & octal number system to decimal number system? > > Thanks, > > Arpan > 'definition' (assignment) of numeric values. The number itself never changes - it is in the native format of the datatype it is stored in. Dim lJunk as Long lJunk = &H0034& Debug.Print "Decimal value is: " & CLng(lJunk) or any of another dozen different ways. -ralph > VB6 has in-built functions to convert numbers from decimal Concatenate "&H" onto the front of your hex value (as a String)number > system to hexadecimal & octal number system (Hex & Oct respectively) > but how do we go the other way round i.e. how to convert numbers from > hexadecimal & octal number system to decimal number system? and then apply the Val function to it... HexValue = "7B" Print Val("&H" & HexValue) 123 Rick
Show quote
Hide quote
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> And "&0" for octalswrote in message news:OVL8vkY1FHA.3188@TK2MSFTNGP14.phx.gbl... > > VB6 has in-built functions to convert numbers from decimal > number > > system to hexadecimal & octal number system (Hex & Oct > respectively) > > but how do we go the other way round i.e. how to convert numbers > from > > hexadecimal & octal number system to decimal number system? > > Concatenate "&H" onto the front of your hex value (as a String) > and then apply the Val function to it... > > HexValue = "7B" > Print Val("&H" & HexValue) > 123 > > Rick > OctalValue = "31" Debug.Print Val("&0" & OctalValue) 25 -ralph "Ralph" <nt_consultin***@yahoo.com> wrote in message &O, not &0.news:kMOdnSt3iNvdJsreRVn-hA@arkansas.net... > And "&0" for octals "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message Actually either will work.news:eEBVO8Y1FHA.1252@TK2MSFTNGP09.phx.gbl... > > "Ralph" <nt_consultin***@yahoo.com> wrote in message > news:kMOdnSt3iNvdJsreRVn-hA@arkansas.net... > > > And "&0" for octals > > &O, not &0. > -ralph Ralph wrote:
>>> And "&0" for octals Wow. Okay, I can go home now... :-)>> >> &O, not &0. > > Actually either will work. Ralph wrote:
> For some definition of "work"... :)> "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message > news:eEBVO8Y1FHA.1252@TK2MSFTNGP09.phx.gbl... > > > > "Ralph" <nt_consultin***@yahoo.com> wrote in message > > news:kMOdnSt3iNvdJsreRVn-hA@arkansas.net... > > > > > And "&0" for octals > > > > &O, not &0. > > > > Actually either will work. Actually, it appears that its actually the leading "&" w/o either H or O is interpreted as octal. I wasn't aware of the leading & being interpreted by VB as octal having always written the "O" on the assumption it was necessary. I could find nothing in the help files mentioning this "feature". > > > > And "&0" for octals either H or O> > > > > > &O, not &0. > > > > > > > Actually either will work. > > For some definition of "work"... :) > > Actually, it appears that its actually the leading "&" w/o > is interpreted as octal. I wasn't aware of the leading & being I think it (ampersand in front of a number) dates back to, and has> interpreted by VB as octal having always written the "O" on the > assumption it was necessary. I could find nothing in the help files > mentioning this "feature". been carried forward from, the earliest days of BASIC, which in turn carried forward the CP/M standard (that probably carried forth an even earlier system<g>) where things were measured in Octal. I think the &O was added when hex made its inroads. Rick Duane Bozarth wrote:
> Been that way forever in Basic, but it should still be in the docs, yeah.> Actually, it appears that its actually the leading "&" w/o either H > or O is interpreted as octal. I wasn't aware of the leading & being > interpreted by VB as octal having always written the "O" on the > assumption it was necessary. I could find nothing in the help files > mentioning this "feature". One of the more insidious octal holdovers and surprises, unless you grew up in C, I suppose, is that an IP address like "122.123.124.44" is NOT the same as 122.123.124.044, because any quad that starts with a zero is assumed to be octal. So, x.x.x.044 = x.x.x.36. And yet you can type 122.123.124.099 and it won't be rejected as illegal -- just silently translated to x.x.x.81 (which makes a perverse sense). Use this fun fact to confuse and delight your friends. -- Jim
Show quote
Hide quote
"Duane Bozarth" <dpboza***@swko.dot.net> wrote in message I actually knew that &xxx meant octal*, but I didn't stop to think that &0 news:4357DC5A.2BBAE53D@swko.dot.net... >> > > And "&0" for octals >> > >> > &O, not &0. >> > >> >> Actually either will work. > > For some definition of "work"... :) > > Actually, it appears that its actually the leading "&" w/o either H or O > is interpreted as octal. I wasn't aware of the leading & being > interpreted by VB as octal having always written the "O" on the > assumption it was necessary. I could find nothing in the help files > mentioning this "feature". was simply & with a leading 0. Makes sense in retrospect. *I'm not sure HOW I knew, because as you said, it doesn't appear to be covered in MSDN. Maybe in older versions of VB help it was there. Or maybe I knew it from BASIC. "Jeff Johnson [MVP: VB]" wrote: The only place I could find it mentioned in VB5 help files was in Oct()> .... > I actually knew that &xxx meant octal*, but I didn't stop to think that &0 > was simply & with a leading 0. Makes sense in retrospect. > > *I'm not sure HOW I knew, because as you said, it doesn't appear to be > covered in MSDN. Maybe in older versions of VB help it was there. Or maybe I > knew it from BASIC. where it says to use "&O" to indicate octal. After being reminded, I think I do recall early BASIC w/ the "feature" but age has intervened since... :) I can't think of the last piece of hardware I dealt w/ that had an octal orientation (lots of hex, but no octal) so my use of Oct() has been quite minimal over at least the last 10-15 years.... :) > > I actually knew that &xxx meant octal*, but I didn't stop to think that &0> > was simply & with a leading 0. Makes sense in retrospect. appear to be> > > > *I'm not sure HOW I knew, because as you said, it doesn't > > covered in MSDN. Maybe in older versions of VB help it was there. Or maybe I> > knew it from BASIC. The reference is the same as that in my copy of VB2, so it appears> > The only place I could find it mentioned in VB5 help files was in Oct() > where it says to use "&O" to indicate octal. that VB stopped mentioning the & sign by itself for Octal representation for quite awhile. I don't have VB1, so I can't check that far back in the VB world; but I have a copy of a book that came with a Sperry computer (IBM PC compatible from way back when) entitled "Programming in BASIC for DOS 2.11" which has this description for "Octal constants"... Digits in the octal numbering system. Octal constants have prefix &O or & followed by up to 6 digits (0-7 inclusive). Examples: &O347, &1234 so the original BASICs were still mentioning it. Rick "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> Sometime's it's good to be an old fart.wrote in message news:Onbi0rb1FHA.2964@TK2MSFTNGP09.phx.gbl... > so the original BASICs were still mentioning it. "Jeff Johnson [MVP: VB]" wrote: If this o-f still had a semblance of a memory, that might be... :)> > "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> > wrote in message news:Onbi0rb1FHA.2964@TK2MSFTNGP09.phx.gbl... > > > so the original BASICs were still mentioning it. > > Sometime's it's good to be an old fart. |
|||||||||||||||||||||||