converting HEX string to ascii string

tchadwick71

New Member
Joined
Jul 31, 2018
Messages
1
I saw your example converting hex to ascii "=CHAR(HEX2DEC("4A"))".
I would like to know how to convert a string of hex to ascii. There is example: 5238304d33335446462 and convert to R80M33TFF
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,510
Office Version
2010
Platform
Windows
I saw your example converting hex to ascii "=CHAR(HEX2DEC("4A"))".
I would like to know how to convert a string of hex to ascii. There is example: 5238304d33335446462 and convert to R80M33TFF
Your example has an odd number of characters in it (total length equals 19 characters)... your conversion seems to ignore the lone 2 at the end. Assuming you will always pass an even number of hex digits, here is a UDF (user defined function) that you can use...
Code:
[table="width: 500"]
[tr]
	[td]Function HEX2ASCII(S As String) As String
  Dim X As Long
  For X = 1 To Len(S) Step 2
    HEX2ASCII = HEX2ASCII & Chr("&H" & Mid(S, X, 2))
  Next
End Function[/td]
[/tr]
[/table]
If you want to be able to pass an odd number of characters and have the odd last character ignored (like your example does), then use this UDF instead...
Code:
[table="width: 500"]
[tr]
	[td]Function HEX2ASCII(ByVal S As String) As String
  Dim X As Long
  If Len(S) Mod 2 Then S = Left(S, Len(S) - 1)
  For X = 1 To Len(S) Step 2
    HEX2ASCII = HEX2ASCII & Chr("&H" & Mid(S, X, 2))
  Next
End Function[/td]
[/tr]
[/table]
HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use HEX2ASCII just like it was a built-in Excel function. For example,

=HEX2ASCII(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:

Forum statistics

Threads
1,082,342
Messages
5,364,783
Members
400,815
Latest member
gangstar67

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top