Home All Groups Group Topic Archive Search About
Author
20 Oct 2005 2:50 PM
Arpan
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

Author
20 Oct 2005 2:56 PM
Duane Bozarth
Arpan wrote:
>
> 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?
>

You mean something like

?val("&H" & hex$(10))
10
Author
20 Oct 2005 3:24 PM
Arpan
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
Author
20 Oct 2005 3:42 PM
Duane Bozarth
Arpan wrote:
>
> 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.

That's <exactly> what I demonstrated...

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
> Oct(10). Now I want a function that will convert the octal 12 back to
> decimal 10.

Same thing...

> 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.
Author
20 Oct 2005 3:54 PM
Duane Bozarth
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 ..."
Author
20 Oct 2005 5:49 PM
Scott Beckstead
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 ***
Author
20 Oct 2005 5:56 PM
Duane Bozarth
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....
Author
20 Oct 2005 10:37 PM
Duane Bozarth
Duane Bozarth wrote:
>
> 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....

And I'm blaming the bifocals... :)
Author
20 Oct 2005 3:14 PM
Ralph
"Arpan" <arpan***@hotmail.com> wrote in message
news: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
>

The routines you are talking about concern the 'presentation' and the
'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
Author
20 Oct 2005 3:16 PM
Rick Rothstein [MVP - Visual Basic]
> 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
Author
20 Oct 2005 3:44 PM
Ralph
Show quote Hide quote
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote 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
>

And "&0" for octals
OctalValue = "31"
Debug.Print Val("&0" & OctalValue)
25

-ralph
Author
20 Oct 2005 3:58 PM
Jeff Johnson [MVP: VB]
"Ralph" <nt_consultin***@yahoo.com> wrote in message
news:kMOdnSt3iNvdJsreRVn-hA@arkansas.net...

> And "&0" for octals

&O, not &0.
Author
20 Oct 2005 5:17 PM
Ralph
"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.

-ralph
Author
20 Oct 2005 5:46 PM
Karl E. Peterson
Ralph wrote:
>>> And "&0" for octals
>>
>> &O, not &0.
>
> Actually either will work.

Wow.  Okay, I can go home now...  :-)
--
Working Without a .NET?
http://classicvb.org/petition
Author
20 Oct 2005 6:05 PM
Duane Bozarth
Ralph wrote:
>
> "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.

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".
Author
20 Oct 2005 6:58 PM
Rick Rothstein [MVP - Visual Basic]
> > > > 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".

I think it (ampersand in front of a number) dates back to, and has
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
Author
20 Oct 2005 6:59 PM
Jim Mack
Duane Bozarth wrote:
>
> 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".

Been that way forever in Basic, but it should still be in the docs, yeah.

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
Author
20 Oct 2005 7:19 PM
Jeff Johnson [MVP: VB]
Show quote Hide quote
"Duane Bozarth" <dpboza***@swko.dot.net> wrote in message
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".

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.
Author
20 Oct 2005 8:18 PM
Duane Bozarth
"Jeff Johnson [MVP: VB]" wrote:
>
....
> 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.

The only place I could find it mentioned in VB5 help files was in Oct()
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.... :)
Author
20 Oct 2005 9:12 PM
Rick Rothstein [MVP - Visual Basic]
> > 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.
>
> The only place I could find it mentioned in VB5 help files was
in Oct()
> where it says to use "&O" to indicate octal.

The reference is the same as that in my copy of VB2, so it appears
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
Author
20 Oct 2005 9:33 PM
Jeff Johnson [MVP: VB]
"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.
Author
20 Oct 2005 10:36 PM
Duane Bozarth
"Jeff Johnson [MVP: VB]" wrote:
>
> "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.

If this o-f still had a semblance of a memory, that might be... :)