Public Function UTF8FromUTF16(ByRef abytUTF16() As Byte) As Byte()
Dim lngByteNum As Long
Dim abytUTF8() As Byte
Dim lngCharCount As Long
On Error GoTo ConversionErr
lngCharCount = (UBound(abytUTF16) + 1) \ 2
lngByteNum = WideCharToMultiByteArray(CP_UTF8, 0, abytUTF16(0), _
lngCharCount, 0, 0, 0, 0)
If lngByteNum > 0 Then
ReDim abytUTF8(lngByteNum - 1)
lngByteNum = WideCharToMultiByteArray(CP_UTF8, 0, abytUTF16(0), _
lngCharCount, abytUTF8(0), lngByteNum, 0, 0)
UTF8FromUTF16 = abytUTF8
End If
Exit Function
ConversionErr:
MsgBox " Conversion failed "
End Function
var
abytUTF8 : array of Byte; // Global
function UTF8FromUTF16(sUTF16 : WideString) : pAnsiChar;
var
lngByteNum : integer;
lngCharCount : integer;
begin
// On Error GoTo ConversionErr
result := nil;
lngCharCount := Length(sUTF16);
lngByteNum := WideCharToMultiByte(CP_UTF8, 0, @sUTF16[1],
lngCharCount, nil, 0, nil, nil);
If lngByteNum > 0 Then
begin
SetLength(abytUTF8, lngByteNum+1);
abytUTF8[lngByteNum] := 0;
lngByteNum := WideCharToMultiByte(CP_UTF8, 0, @sUTF16[1],
lngCharCount, @abytUTF8[0], lngByteNum, nil, nil);
result := pAnsiChar(@abytUTF8[0]);
End;
End;