Tuesday, June 22, 2010

Visual Basic UTF8

After much googling and messing about I have some UTF8 conversion functions for Visual Basic (vb).

Option Compare Database
Public Const UTF8 = 65001
Public Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Public Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long

'
'Converts Unicode to UTF8. It wraps the system call WideCharToMultiByte.
'
Public Function UTF8_Encode_System(ByVal sStr As String) As String
    Dim buffer As String
    Dim length As Long
    
    'Get the length of the converted data.
    length = WideCharToMultiByte(UTF8, 0, StrPtr(sStr), Len(sStr), 0, 0, 0, 0)
    
    'Ensure the buffer is the correct size.
    buffer = String$(length, 0)
    
    'Convert the string into the buffer.
    length = WideCharToMultiByte(UTF8, 0, StrPtr(sStr), Len(sStr), StrPtr(buffer), Len(buffer), 0, 0)
    
    'Access needs it in unicode?
    buffer = StrConv(buffer, vbUnicode)
    
    'Chop of any crap.
    buffer = Left$(buffer, length)
    
    'Return baby.
    UTF8_Encode_System = buffer
End Function

'
'Converts UTF8 data into unicode. It wrapps the system call MultiByteToWideChar.
'
Public Function UTF8_Decode_System(ByVal sStr As String) As String
    Dim buffer As String
    Dim length As Long
    
    'Get the length of the converted data.
    length = MultiByteToWideChar(UTF8, 0, StrPtr(StrConv(sStr, vbFromUnicode)), Len(sStr), 0, 0)
    
    'Ensure the buffer is the correct size.
    buffer = String$(length, 0)
    
    'Convert the data into the buffer.
    length = MultiByteToWideChar(UTF8, 0, StrPtr(StrConv(sStr, vbFromUnicode)), Len(sStr), StrPtr(buffer), Len(buffer))
    
    'Chop off any crap.
    buffer = Left$(buffer, length)
    
    'Return baby.
    UTF8_Decode_System = buffer
End Function

3 comments:

  1. Thanks for this - very helpful!

    I think MultiByteToWideChar is missing in the declaration though (line 4).

    ReplyDelete
  2. Hi there, not sure if youre still around to answer this but this code is pretty much what ive spent a week searching for. I'm having some problems however. I get errors for the use of Option Compare Database, strptr() and vbFromUnicode. I'm pretty new to programming and I'm self taught, I'm not sure if there is something special I have to do to get this to work or if it will even still work with updated visual basic . net

    ReplyDelete