Hex to Dec


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Hex to Dec

  1. #1
    Martin Boulianne Guest

    Hex to Dec


    Is there a "VB native" function to convert an hexadecimal nb into a decimal?

  2. #2
    Phil Weber Guest

    Re: Hex to Dec

    > Is there a "VB native" function to convert a hexadecimal
    > number into decimal?


    Martin: Try the Val function:

    sHex = "&H1A2B"
    Debug.Print Val(sHex)

    ---
    Phil Weber



  3. #3
    Mattias Sjögren Guest

    Re: Hex to Dec

    Martin,

    >Is there a "VB native" function to convert an hexadecimal nb into a decimal?


    Basicly any conversion function, such as CLng, CInt, Val, can do that.

    dec = CLng("&H" & hex)


    Matt

    ============================================
    Mattias Sjögren - mattiass @ hem.passagen.se
    VB+ - http://hem.spray.se/mattias.sjogren/
    CodeHound - Free VB Search Engine
    http://www.codehound.com

  4. #4
    Rick Rothstein Guest

    Re: Hex to Dec

    Phil . . . hopefully Martin doesn't have any hex
    numbers equal to or larger than "&8000" <g>

    Martin . . . use CLng instead of Val.

    Rick


    "Phil Weber" <pweber@devx.com> wrote in message
    news:39762043$1@news.devx.com...
    > > Is there a "VB native" function to convert a hexadecimal
    > > number into decimal?

    >
    > Martin: Try the Val function:
    >
    > sHex = "&H1A2B"
    > Debug.Print Val(sHex)
    >
    > ---
    > Phil Weber
    >
    >




  5. #5
    Phil Weber Guest

    Re: Hex to Dec

    > Hopefully Martin doesn't have any hex numbers
    > equal to or larger than "&H8000" <g>


    Rick: Good point. Val() works fine with such numbers, but the result may not
    be what one expects. Since a four-digit hex number contains 32 bits (two
    bytes), Val() returns the decimal representation as a two-byte Integer.
    Since VB's Integers are signed, hex values "greater" than &H8000 are
    returned as negative decimal values. If you want a positive value, CLng will
    work for hex numbers up to &H7FFFFFFF.
    ---
    Phil Weber



  6. #6
    Eric D. Burdo Guest

    Re: Hex to Dec

    There was a topic on this a while ago, and after much deliberation, the
    final answer was:

    You can use the Val function:

    Dim l As Long
    Dim sHex As String

    sHex = "A0"
    l = Val("&H" & sHex & "&")

    The leading "&H" tells Val that your value is in hexadecimal. The trailing
    "&" is to force the value to be of type Long, instead of type Integer.
    Without it, "FFFF" would be interpreted as -1 instead of 65535


    I do not remember who posted this final answer, so credits go to whoever it
    was...

    --

    Eric D. Burdo, Red-Leif International
    VB Programmer and Consultant
    <http://www.redleif.com/vb>

    *** Please reply to the newsgroup so all can benefit. ***


    "Martin Boulianne" <kaaboum@hotmail.com> wrote in message
    news:39761fb0$1@news.devx.com...
    >
    > Is there a "VB native" function to convert an hexadecimal nb into a

    decimal?



  7. #7
    Rick Rothstein Guest

    Re: Hex to Dec

    But isn't CLng easier to remember compared to remembering to concatenate the
    extra ampersand at the end of the hex number?

    And besides, who wants to look at an argument containing four ampersands
    representing nearly 25% (using your sHex variable name) of the characters
    typed <g>

    Rick


    "Eric D. Burdo" <vbtips@redleif.com> wrote in message
    news:3976f378$1@news.devx.com...
    > There was a topic on this a while ago, and after much deliberation, the
    > final answer was:
    >
    > You can use the Val function:
    >
    > Dim l As Long
    > Dim sHex As String
    >
    > sHex = "A0"
    > l = Val("&H" & sHex & "&")
    >
    > The leading "&H" tells Val that your value is in hexadecimal. The trailing
    > "&" is to force the value to be of type Long, instead of type Integer.
    > Without it, "FFFF" would be interpreted as -1 instead of 65535
    >
    >
    > I do not remember who posted this final answer, so credits go to whoever

    it
    > was...
    >
    > --
    >
    > Eric D. Burdo, Red-Leif International
    > VB Programmer and Consultant
    > <http://www.redleif.com/vb>
    >
    > *** Please reply to the newsgroup so all can benefit. ***
    >
    >
    > "Martin Boulianne" <kaaboum@hotmail.com> wrote in message
    > news:39761fb0$1@news.devx.com...
    > >
    > > Is there a "VB native" function to convert an hexadecimal nb into a

    > decimal?
    >
    >




  8. #8
    mrfelis Guest

    Re: Hex to Dec

    Rick Rothstein <rick_newsgroup@email.com> wrote in message
    news:3976ffb8$1@news.devx.com...
    > But isn't CLng easier to remember compared to remembering to concatenate

    the
    > extra ampersand at the end of the hex number?
    >
    > And besides, who wants to look at an argument containing four ampersands
    > representing nearly 25% (using your sHex variable name) of the characters
    > typed <g>
    >
    > Rick

    Give me unsigned intergers and I wouldn't care!

    --
    ~~~
    C'Ya,
    mrfelis
    mrfelis@yahoo.NOSPAM.com
    just remove the spam
    >
    >
    > "Eric D. Burdo" <vbtips@redleif.com> wrote in message
    > news:3976f378$1@news.devx.com...
    > > There was a topic on this a while ago, and after much deliberation, the
    > > final answer was:
    > >
    > > You can use the Val function:
    > >
    > > Dim l As Long
    > > Dim sHex As String
    > >
    > > sHex = "A0"
    > > l = Val("&H" & sHex & "&")
    > >
    > > The leading "&H" tells Val that your value is in hexadecimal. The

    trailing
    > > "&" is to force the value to be of type Long, instead of type Integer.
    > > Without it, "FFFF" would be interpreted as -1 instead of 65535
    > >
    > >
    > > I do not remember who posted this final answer, so credits go to whoever

    > it
    > > was...
    > >
    > > --
    > >
    > > Eric D. Burdo, Red-Leif International
    > > VB Programmer and Consultant
    > > <http://www.redleif.com/vb>
    > >
    > > *** Please reply to the newsgroup so all can benefit. ***
    > >
    > >
    > > "Martin Boulianne" <kaaboum@hotmail.com> wrote in message
    > > news:39761fb0$1@news.devx.com...
    > > >
    > > > Is there a "VB native" function to convert an hexadecimal nb into a

    > > decimal?
    > >
    > >

    >
    >




  9. #9
    Larry Serflaten Guest

    Re: Hex to Dec

    Currency types don't cut it for you?

    LFS

    "mrfelis" <mrfelis@yahoo.NOSPAM.com> wrote in message news:39773bf7$1@news.devx.com...
    > Give me unsigned intergers and I wouldn't care!





  10. #10
    mrfelis Guest

    Re: Hex to Dec

    Nope.

    Cause if MS ever sees fit to give VB unsigned integers, that's one less step
    away from Shift operators for those unsigned data types.

    --
    ~~~
    C'Ya,
    mrfelis
    mrfelis@yahoo.NOSPAM.com
    just remove the spam
    Larry Serflaten <serflaten@usinternet.com> wrote in message
    news:397759e3@news.devx.com...
    > Currency types don't cut it for you?
    >
    > LFS
    >
    > "mrfelis" <mrfelis@yahoo.NOSPAM.com> wrote in message

    news:39773bf7$1@news.devx.com...
    > > Give me unsigned integers and I wouldn't care!

    >
    >
    >




  11. #11
    Alan Gillott Guest

    Re: Hex to Dec

    I wonder if we have missed the point...

    How do you convert Text representation of hex into any form of decimal. As
    everyone implies, the compiler converts Hex to decimal within the bounds of
    the compiler but what if I have an INI file containing Hex?

    [My section]
    MyHex=3e5bc

    Now let's be helpful...

    Source=GetIni(......."MyHex"....)
    Number=0
    For i=1 to Len(Source)
    If IsNumeric(Mid(Source,i,1)) then
    Number=Number + Len(Source)-i * 16 * CInt(Mid(Source,i,1)
    Else
    Select Case Mid(Source,i,1)
    Case "A","a"
    tmp=10
    Case "B","b"
    tmp=11
    etc.....
    Case Else
    MsgBox "Complain Bitterly"
    End select
    Number=Number + Len(Source)-i * 16 * tmp
    end If
    Next

    Note: Not Tested...

    mrfelis <mrfelis@yahoo.NOSPAM.com> wrote in message
    news:39776b28$1@news.devx.com...
    > Nope.
    >
    > Cause if MS ever sees fit to give VB unsigned integers, that's one less

    step
    > away from Shift operators for those unsigned data types.
    >
    > --
    > ~~~
    > C'Ya,
    > mrfelis
    > mrfelis@yahoo.NOSPAM.com
    > just remove the spam
    > Larry Serflaten <serflaten@usinternet.com> wrote in message
    > news:397759e3@news.devx.com...
    > > Currency types don't cut it for you?
    > >
    > > LFS
    > >
    > > "mrfelis" <mrfelis@yahoo.NOSPAM.com> wrote in message

    > news:39773bf7$1@news.devx.com...
    > > > Give me unsigned integers and I wouldn't care!

    > >
    > >
    > >

    >
    >




  12. #12
    bob butler Guest

    Re: Hex to Dec


    Alan Gillott <agillott@compuserve.com> wrote in message
    news:397b45e0@news.devx.com...
    > I wonder if we have missed the point...
    >
    > How do you convert Text representation of hex into any form of decimal. As
    > everyone implies, the compiler converts Hex to decimal within the bounds

    of
    > the compiler but what if I have an INI file containing Hex?
    >
    > [My section]
    > MyHex=3e5bc
    >
    > Now let's be helpful...
    >
    > Source=GetIni(......."MyHex"....)


    Number=CLng("&H" & Source)




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center