VB 6.0-> Delphi XE2转换 [英] VB 6.0 -> Delphi XE2 Conversion
本文介绍了VB 6.0-> Delphi XE2转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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;
推荐答案
您的代码未设置结果字符串的编码. Delphi(自Delphi 2009起)要求ANSI字符串的编码信息,否则使用默认的系统区域设置.您的代码的有效版本为:
Your code does not set encoding of the resulting string. Delphi (since Delphi 2009) requires encoding info for ANSI string, otherwise default system locale used. A working version of your code is:
function UTF8FromUTF16(sUTF16: UnicodeString): UTF8String;
var
lngByteNum : integer;
lngCharCount : integer;
begin
Result := '';
lngCharCount := Length(sUTF16);
if lngCharCount = 0 then Exit;
lngByteNum := WideCharToMultiByte(CP_UTF8, 0, @sUTF16[1], lngCharCount, nil, 0, nil, nil);
if lngByteNum > 0 then begin
SetLength(Result, lngByteNum);
WideCharToMultiByte(CP_UTF8, 0, @sUTF16[1], lngCharCount, @Result[1], lngByteNum, nil, nil);
end;
end;
但是您并不需要全部-Delphi为您执行字符串转换:
But you need not that all - Delphi performs string conversions for you:
function UTF8FromUTF16_2(sUTF16: UnicodeString): UTF8String;
begin
Result := sUTF16;
end;
这篇关于VB 6.0-> Delphi XE2转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文