【发布时间】:2015-08-10 14:22:46
【问题描述】:
我使用 Delphi 7(基于 ANSI)。我需要在代码页之间转换字符串。我在网上找到了一个带有 multiByteToWideChar 和 wideCharToMultiByte 函数的解决方案。但正如我所见,它不起作用。我可以从 1250 转换为 1252,但另一种方式不太好。 这是我的测试代码:
procedure TForm1.Button1Click(Sender: TObject);
function ANSIToUTF8( text_ : string; codePage_ : cardinal ): string;
var
w : WideString;
sizeMB, sizeWC : integer;
begin
if ( codePage_ <> CONST_codepage_UTF8 ) then
begin
// ANSI_XXXX to UTF16
sizeMB := length( text_ );
sizeWC := multiByteToWideChar( codePage_, 0, PAnsiChar( text_ ), sizeMB, nil, 0 );
setLength( w, sizeWC );
multiByteToWideChar( codePage_, 0, PAnsiChar( text_ ), sizeMB, PWideChar( w ), sizeWC );
// UTF16 to UTF8
sizeMB := wideCharToMultiByte( CONST_codepage_UTF8, 0, PWideChar( w ), sizeWC, nil, 0, nil, nil );
setLength( result, sizeMB );
wideCharToMultiByte( CONST_codepage_UTF8, 0, PWideChar( w ), sizeWC, PAnsiChar( Result ), sizeMB, nil, nil );
end else
result := text_;
end;
function UTF8ToANSI( text_ : string; codePage_ : cardinal ): string;
var
w : WideString;
sizeMB, sizeWC : integer;
begin
if ( codePage_ <> CONST_codepage_UTF8 ) then
begin
// UTF8 to UTF16
sizeMB := length( text_ );
sizeWC := multiByteToWideChar( CONST_codepage_UTF8, 0, PAnsiChar( text_ ), sizeMB, nil, 0 );
setLength( w, sizeWC );
multiByteToWideChar( CONST_codepage_UTF8, 0, PAnsiChar( text_ ), sizeMB, PWideChar( w ), sizeWC );
// UTF16 to ANSI_XXXX
sizeMB := wideCharToMultiByte( codePage_, 0, PWideChar( w ), sizeWC, nil, 0, nil, nil );
setLength( result, sizeMB );
wideCharToMultiByte( codePage_, 0, PWideChar( w ), sizeWC, PAnsiChar( Result ), sizeMB, nil, nil );
end else
result := text_;
end;
procedure testString( s_ : string; icp_ : cardinal );
var
sutf : string;
s1250, s1252 : string;
pc : pchar;
function strToHex( s_ : string; ocp_ : cardinal ) : string;
var
i : integer;
begin
result := '';
for i := 1 to length( s_ ) do
begin
if ( i > 1 ) then
result := result + ', ';
result := result + TStringUtility.byteToHexaDecimalStr( ord( s_[i] ) );
end;
end;
procedure logInput;
var
s : string;
begin
s := 'Input (' + intToStr( icp_ ) + '): ' + strToHex( s_, icp_ );
listbox1.items.add( s );
end;
procedure logOutput( ocp_ : cardinal );
var
s : string;
begin
s_ := utf8toansi( sutf, ocp_ );
s := 'Output (' + intToStr( ocp_ ) + '): ' + strToHex( s_, ocp_ );
listbox1.items.add( s );
end;
begin
logInput;
sutf := ansitoutf8( s_, icp_ );
logOutput( 1250 );
logOutput( 1252 );
listbox1.items.add( '' );
end;
begin
testString( #$f5 + #$fa + #$fb, 1250 ); // õúû in 1250
testString( #$6f + #$fa + #$75, 1252 ); // õúû in 1252
end;
记录的结果不是专家的结果。它显示api调用将字符串从1250转换为1252,但没有将1252转换为1250。我将默认代码页更改为1252,结果相同。
Input (1250): $f5, $fa, $fb
Output (1250): $f5, $fa, $fb
Output (1252): $6f, $fa, $75
Input (1252): $6f, $fa, $75
Output (1250): $6f, $fa, $75
Output (1252): $6f, $fa, $75
【问题讨论】:
-
几十年前通过引入 Unicode 解决了这个问题。
-
FWIW、
#$6F和#$75是纯 ASCII,在所有编码中可能都相同。但是用#$F5和#$FB编码的码位在1252中似乎不存在,所以它们变成了纯ASCII值。