Search code examples
vbaunicodestring-conversion

Convert an Unicode Hexadecimal String in VBA


I have some string data stored in a database which special characters are represented as Unicode Hexadecimal. I would like to convert the data.

Does anyone know how to do that in VBA without by replacing each special character.

e.g: The conversion I'm expecting is:

Opera\'e7\'e3o -> Operação

Thank you!


Solution

  • Try this sample code:

    Dim myStr
    Dim nStart,nLen, sTmp
    myStr = "Opera\'e7\'e3o"
    nStart = 0
    nLen = Len(myStr)
    
    While nStart < nLen
        nStart = Instr(nStart+1,myStr,"\'")
        If nStart = 0 Then
            nStart = nLen
        Else
            sTmp = Mid(myStr,nStart,4)
            myStr = Replace(myStr,sTmp,Chr(CLng(Replace(sTmp,"\'","&h"))))
        End If
    Wend
    MsgBox myStr
    

    Costis Papadakis