Hex and String Conversions
Here is my Hex to String function :)
Hex to String Function Code:
Public Function HexToString(ByVal HexToStr As String) As String
Dim strTemp As String
Dim strReturn As String
Dim I As Long
For I = 1 To Len(HexToStr) Step 3
strTemp = Chr$(Val("&H" & Mid$(HexToStr, I, 2)))
strReturn = strReturn & strTemp
Next I
HexToString = strReturn
End Function
and here is my String to Hex function :)
String to Hex Function Code:
Public Function StringToHex(ByVal StrToHex As String) As String
Dim strTemp As String
Dim strReturn As String
Dim I As Long
For I = 1 To Len(StrToHex)
strTemp = Hex$(Asc(Mid$(StrToHex, I, 1)))
If Len(strTemp) = 1 Then strTemp = "0" & strTemp
strReturn = strReturn & Space$(1) & strTemp
Next I
StringToHex = strReturn
End Function
I hope they help everyone :)
Re: Hex and String Conversions
in the hex to string converter, that for should have a step of 2, not 3. Hope that helps someone cause it drove me nuts:)
Re: Hex and String Conversions
3 will do perfectly fine.
Re: Hex and String Conversions
3 just gave me gibberish, but 2 works great...weird:)
Re: Hex and String Conversions
3 works fine for me. 2 gives me something entirely off.
Re: Hex and String Conversions
You're giving different strings: the code in first post works with space separated codes.
Hex strings as such aren't very useful though.
Re: Hex and String Conversions
That makes sense, there are no spaces in my hex string.
Re: Hex and String Conversions
Quote:
Originally Posted by
Paul M
Here is my Hex to String function :)
I hope they help everyone :)
Code:
For anybody wondering, here it is for Excel VBA
Public Function HexToString(ByVal HexToStr As String) As String
Dim strTemp As String
Dim strReturn As String
Dim I As Long
For I = 1 To Len(HexToStr) Step 2 'edit: 2 instead of 3
strTemp = Chr$(Val("&H" & Mid$(HexToStr, I, 2)))
strReturn = strReturn & strTemp
Next I
HexToString = Right(strReturn, Len(strReturn) - 1) 'edit: removes first character (else printed value is empty)
End Function
Sub TranslateAllHex()
Dim aCell As Range
Dim aCellsValue As String
Dim OutputString As String
For Each aCell In Selection
aCellsValue = aCell.Value
If Not aCellsValue = "" Then
If Left(aCellsValue, 2) = "0x" Then
aCell.Value = HexToString(aCell.Value)
End If
End If
Next aCell
End Sub
Re: Hex and String Conversions
Since this old thread has been resurrected, let's note that all of the above make the assumption that the hex is representing an ANSI string, and not a Unicode string. A character in a Unicode string would take 4 hex characters to be represented (not 2). The Chr$(...) will convert to Unicode, but the incoming hex string must represent ANSI.
Also, the StringToHex function will return gibberish if the incoming string contains any true Unicode characters (ASC(char) > 255).
To be done correctly, one might consider an optional argument that specifies ANSI or Unicode. Also, an "ASC(char) AND 255" might be warranted to keep StringToHex from returning gibberish if ANSI is specified.
Also, this could all be much better done with a byte array, which would eliminate so much thrashing around with temp strings and concatenations.
Best Regards,
Elroy