|
-
May 4th, 2007, 01:06 AM
#1
Thread Starter
Interweb adm/o/distrator
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
-
May 10th, 2007, 01:15 AM
#2
New Member
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
-
May 10th, 2007, 01:24 AM
#3
Thread Starter
Interweb adm/o/distrator
Re: Hex and String Conversions
3 will do perfectly fine.
-
May 10th, 2007, 04:53 AM
#4
New Member
Re: Hex and String Conversions
3 just gave me gibberish, but 2 works great...weird
-
May 10th, 2007, 06:38 AM
#5
Junior Member
Re: Hex and String Conversions
3 works fine for me. 2 gives me something entirely off.
-
May 10th, 2007, 10:42 AM
#6
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.
-
May 10th, 2007, 12:15 PM
#7
New Member
Re: Hex and String Conversions
That makes sense, there are no spaces in my hex string.
-
Aug 7th, 2017, 06:08 AM
#8
New Member
Re: Hex and String Conversions
 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
-
Aug 12th, 2017, 10:02 AM
#9
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
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|