# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Hex and String Conversions

## Paul M

Here is my Hex to String function  :Smilie: 


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  :Smilie: 


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  :Smilie:

----------


## thenowhereman

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 :Smilie:

----------


## Paul M

3 will do perfectly fine.

----------


## thenowhereman

3 just gave me gibberish, but 2 works great...weird :Smilie:

----------


## vbleet

3 works fine for me. 2 gives me something entirely off.

----------


## Merri

You're giving different strings: the code in first post works with space separated codes.

Hex strings as such aren't very useful though.

----------


## thenowhereman

That makes sense, there are no spaces in my hex string.

----------


## JatS

> Here is my Hex to String function 
> 
> I hope they help everyone





```
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
```

----------


## Elroy

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

----------

