Шаблон для тестирования Юникода
Опубликовано krak в Чт, 15.04.2010 - 10:40
Источник: http://imadering.mybb.ru/viewtopic.php?id=64
Может кому то будет полезно увидеть немного "Правильных" функций для работы с юникод строками.
procedure TForm1.Button1Click(Sender: TObject);
type
cp_string = type AnsiString(65001);
var
s_ansi: AnsiString;
s_utf8: utf8string;
s_unicode: string;
s_cp: cp_string;
s_raw: RawByteString;
ts: TStringList;
bb: TBytes;
begin
s_ansi := 'Contacts - Контакты - 世界您好';
s_utf8 := 'Contacts - Контакты - 世界您好';
s_unicode := 'Contacts - Контакты - 世界您好';
s_cp := 'Contacts - Контакты - 世界您好';
//
Label1.Caption := s_ansi + ' > ' + IntToStr(StringCodePage(s_ansi));
Label2.Caption := s_utf8 + ' > ' + IntToStr(StringCodePage(s_utf8));
Label3.Caption := s_unicode + ' > ' + IntToStr(StringCodePage(s_unicode));
//
ts := TStringList.Create;
try
Label4.Caption := s_cp + ' > ' + IntToStr(StringCodePage(s_cp));
ts.Append(s_cp);
Label5.Caption := ts.Text;
s_raw := s_ansi;
SetCodePage(s_raw, 65001, true);
Label6.Caption := s_raw + ' > ' + IntToStr(StringCodePage(s_raw));
finally
ts.Free;
end;
//
bb := TEncoding.Convert(TEncoding.Unicode, TEncoding.UTF8, TEncoding.Unicode.GetBytes(s_unicode));
Label7.Caption := TEncoding.UTF8.GetString(bb);
end;
function Text2Hex(Msg: RawByteString): string;
var
I: Integer;
begin
Result := EmptyStr;
for I := 1 to Length(Msg) do
begin
Result := Result + Inttohex(Ord(Msg[i]), 2);
end;
end;
function Hex2Text(HexText: string): string;
var
I: Integer;
begin
Result := EmptyStr;
for I := 1 to Length(HexText) div 2 do
Result := Result + AnsiChar(StrToInt('$' + Copy(HexText, (I - 1) * 2 + 1, 2)));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
r: RawByteString;
begin
s := Text2Hex(#$4C#$A1);
s := Hex2Text(s);
r := s;
//
Label8.Caption := r;
Label9.Caption := IntToStr(Ord(r[2]));
end;
type
cp_string = type AnsiString(65001);
var
s_ansi: AnsiString;
s_utf8: utf8string;
s_unicode: string;
s_cp: cp_string;
s_raw: RawByteString;
ts: TStringList;
bb: TBytes;
begin
s_ansi := 'Contacts - Контакты - 世界您好';
s_utf8 := 'Contacts - Контакты - 世界您好';
s_unicode := 'Contacts - Контакты - 世界您好';
s_cp := 'Contacts - Контакты - 世界您好';
//
Label1.Caption := s_ansi + ' > ' + IntToStr(StringCodePage(s_ansi));
Label2.Caption := s_utf8 + ' > ' + IntToStr(StringCodePage(s_utf8));
Label3.Caption := s_unicode + ' > ' + IntToStr(StringCodePage(s_unicode));
//
ts := TStringList.Create;
try
Label4.Caption := s_cp + ' > ' + IntToStr(StringCodePage(s_cp));
ts.Append(s_cp);
Label5.Caption := ts.Text;
s_raw := s_ansi;
SetCodePage(s_raw, 65001, true);
Label6.Caption := s_raw + ' > ' + IntToStr(StringCodePage(s_raw));
finally
ts.Free;
end;
//
bb := TEncoding.Convert(TEncoding.Unicode, TEncoding.UTF8, TEncoding.Unicode.GetBytes(s_unicode));
Label7.Caption := TEncoding.UTF8.GetString(bb);
end;
function Text2Hex(Msg: RawByteString): string;
var
I: Integer;
begin
Result := EmptyStr;
for I := 1 to Length(Msg) do
begin
Result := Result + Inttohex(Ord(Msg[i]), 2);
end;
end;
function Hex2Text(HexText: string): string;
var
I: Integer;
begin
Result := EmptyStr;
for I := 1 to Length(HexText) div 2 do
Result := Result + AnsiChar(StrToInt('$' + Copy(HexText, (I - 1) * 2 + 1, 2)));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
r: RawByteString;
begin
s := Text2Hex(#$4C#$A1);
s := Hex2Text(s);
r := s;
//
Label8.Caption := r;
Label9.Caption := IntToStr(Ord(r[2]));
end;
Источник: http://imadering.mybb.ru/viewtopic.php?id=64
Адрес уведомления об обратной ссылке для этой записи:
http://www.kraeg.ru/trackback/10

