Thread: Code: Function lngHexToLong(ByVal strText As Strin

1. 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.
' 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. Re: Code: Function lngHexToLong(ByVal strText As Strin

Nice function. Tell me where you use this. I'm curious.

3. 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
?

4. 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. 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
•