Results 1 to 5 of 5
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Function lngHexToLong(ByVal strText As Strin

    Here is a way of converting a string of hexadecimal (base 16) digit characters to a LONG numeric value.

    Since this is an internal function (called by other procedures, not directly called by the user) I am allowed to make the assumption that the incoming string of hex digits is a valid string; that is, that it is an even number of characters taken from the set 0-9 and A-F.

    Somewhere in between the user call (a Macro) and this procedure must be a procedure that checks that the string is valid.

    I have a related function strHextToChar.

    <pre>Public Function lngHexToLong(ByVal strText As String) As Long
    ' Procedure : lngHexToLong
    ' Description: Convert a string of hexadecimal digits to a LONG value.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING of characters in range 0-9 and A-F.
    ' Returns: LONG.
    ' Assumes: Pairs of valid Hex digits are presented.
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    Dim lngResult As Long
    lngResult = 0
    If Len(strText) Mod 2 <> 0 Then
    strText = "0" & strText
    End If
    Do While Len(strText) > 0
    lngResult = lngResult * 256 + (Val("&H" & Left$(strText, 2)))
    strText = Right$(strText, Len(strText) - 2)
    Loop
    lngHexToLong = lngResult
    'Sub TESTlngHexTolong()
    'MsgBox lngHexToLong("414243") ' 4,276,803
    'End Sub
    End Function
    </pre>


  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function lngHexToLong(ByVal strText As Strin

    Nice function. Tell me where you use this. I'm curious.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Code: Function lngHexToLong(ByVal strText As Strin

    Hi Chris,
    Just out of curiosity, why not something like:
    Function hextolong(strHex As String)
    hextolong = CLng(Val("&H" & strHex))
    End Function
    ?
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function lngHexToLong(ByVal strText As Strin

    It will be in use over the next two days. I'm working up towards a grand finale.

    OK. I use it to test characters within a file that has the potential to be a document.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function lngHexToLong(ByVal strText As Strin

    >CLng(Val("&H" & strHex)

    Because I didn't KNOW about the "&H" prefix and the CVal until others like you posted it here!


    (An Aside: I don't post stuff here for others to learn; I post stuff here very selfishly so that I can learn).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •