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