CNV_UTF8FromLatin1 converts a string of 8-bit Latin-1 characters into UTF-8 format.
Public Declare Function CNV_UTF8FromLatin1 Lib "diCrPKI.dll"
(ByVal strOutput As String, ByVal nOutChars As Long, ByVal strInput As String) As Long
nLen = CNV_UTF8FromLatin1(strOutput, nOutChars, strInput)
String to receive the output.Long specifying the maximum number of characters to be received.String of Latin-1 characters to be decoded.
long _stdcall CNV_UTF8FromLatin1(char *szOutput, long nOutChars, const char *szInput);
Long: If successful, the return value is a positive number indicating the
number of (single-byte) characters in the output string, or number of characters required if nOutChars is zero;
otherwise it returns a negative error code.
Use the in-built method System.Text.Encoding.
Will set up to nOutChars characters in the output string. If nOutChars is zero, it returns the required number of (single-byte) characters.
Dim strData As String Dim strDataUTF8 As String Dim strDataLatin1 As String Dim nRet As Long Dim nLen As Long ' Our original string data is in "Latin-1" encoding strData = "Asociación Mexicana de Estándares para el Comercio Electrónico A.C.|México|" Debug.Print strData ' So convert to UTF-8 nLen = CNV_UTF8FromLatin1("", 0, strData) If nLen < 0 Then Debug.Print "Failed to convert to UTF-8: " & nLen Exit Function End If strDataUTF8 = String(nLen, " ") nLen = CNV_UTF8FromLatin1(strDataUTF8, nLen, strData) ' Which may not display correctly in VB6...! Debug.Print strDataUTF8 ' Now convert back to Latin-1 nLen = CNV_Latin1FromUTF8("", 0, strDataUTF8) strDataLatin1 = String(nLen, " ") nLen = CNV_Latin1FromUTF8(strDataLatin1, nLen, strDataUTF8) Debug.Print strDataLatin1
This should give results like this:
Asociación Mexicana de Estándares para el Comercio Electrónico A.C.|México| Asociación Mexicana de Estándares para el Comercio Electrónico A.C.|México| Asociación Mexicana de Estándares para el Comercio Electrónico A.C.|México|
Note how the second string includes the UTF-8 double-byte characters like "ó" instead of the simple 8-bit Latin-1 character "ó". VB6 doesn't cope well with multibyte UTF-8 characters; .NET is better. To view UTF-8 data properly, you need to use a UTF-8-compatible text editor.
CNV_Latin1FromUTF8 CNV_CheckUTF8