知道双字节码, 如何获取汉字 - 回复 "pinezhou" 的问题

时间:2021-09-09 17:07:23

问题来源: http://www.cnblogs.com/del/archive/2008/09/05/1284923.html#1818187

{查看同一字符串的双字节码和 UniCode 码}
procedure TForm1.Button1Click(Sender: TObject);
var
  str1: AnsiString;
  str2: UnicodeString; {string = UnicodeString}
  P: PINT;
begin
  { "万一" 对应的双字节码是: BBD2F2CD }
  str1 := '万一';
  P := @str1[1];
  ShowMessage(IntToHex(P^, 4)); //BBD2F2CD {万: F2CD; 一:BBD2 }

  { "万一" 对应的 UniCode 码是: 4E004E07 }
  str2 := '万一';
  P := @str2[1];
  ShowMessage(IntToHex(P^, 4)); //4E004E07 {万: 4E00; 一: 4E00}
end;

{知道双字节码, 如何转换为 UniCode: 两个字一起转}
procedure TForm1.Button2Click(Sender: TObject);
var
  str1: AnsiString;
  str2: string;
begin
  SetLength(str1, 4);
  PInteger(@str1[1])^ := $BBD2F2CD;

  Memo1.Lines.Add(str1); //万一
  str2 := str1;
  Memo1.Lines.Add(str2); //万一
end;

{逐字转}
procedure TForm1.Button3Click(Sender: TObject);
var
  str1: AnsiString;
begin
  SetLength(str1, 2);
  PWord(@str1[1])^ := $F2CD;
  Memo1.Lines.Add(str1); //万
  PWord(@str1[1])^ := $BBD2;
  Memo1.Lines.Add(str1); //一
end;

 
 
 

好像没有解决问题, 换个思路重来一下:

const
  GB18030 = 54936;
var
  BytesGB18030: TBytes;

{查看 Hex 的函数}
function ToHex(p: PByteArray; b: Integer): string;
var
  i: Integer;
begin
  for i := 0 to b - 1 do
    Result := IntToHex(p^[i], 2) + Chr(32) + Result;
  Result := TrimRight(Result);
end;

{观察字符串以 GB18030 储存的编码}
procedure TForm1.Button1Click(Sender: TObject);
var
  strHex: string;
  StringStream: TStringStream;
begin
  StringStream := TStringStream.Create('万一的 Delphi 博客', GB18030);
  BytesGB18030 := StringStream.Bytes;
  Memo1.Lines.Add(StringStream.DataString);
  Memo1.Lines.Add(ToHex(@BytesGB18030[0], Length(BytesGB18030)));
  Memo1.Lines.Add('');
  StringStream.Free;
end;

{观察字符串以 Unicode 储存的编码}
procedure TForm1.Button2Click(Sender: TObject);
var
  strHex: string;
  StringStream: TStringStream;
begin
  StringStream := TStringStream.Create('万一的 Delphi 博客', TEncoding.Unicode);
  Memo1.Lines.Add(StringStream.DataString);
  Memo1.Lines.Add(ToHex(@StringStream.Bytes[0], Length(StringStream.Bytes)));
  Memo1.Lines.Add('');
  StringStream.Free;
end;

{把从前面测试得到的字节数组 BytesGB18030 转换为 Unicode 字符串}
procedure TForm1.Button3Click(Sender: TObject);
var
  StringStream: TStringStream;
  str,strHex: string;
begin
  if Length(BytesGB18030) < 1 then Exit;

  {直接根据字节数组建立 TStringStream}
  StringStream := TStringStream.Create(BytesGB18030);
  {TStringStream.DataString 就已经是标准的 string}
  str := StringStream.DataString;

  Memo1.Lines.Add(StringStream.DataString);
  Memo1.Lines.Add(ToHex(@StringStream.Bytes[0], Length(StringStream.Bytes)));
  Memo1.Lines.Add('');
  Memo1.Lines.Add(str);
  Memo1.Lines.Add(ToHex(@str[1], ByteLength(str)));
  StringStream.Free;
end;

 
 
 

(* Memo 中的测试结果:
Memo1
万一的 Delphi 博客
CD BF A9 B2 20 69 68 70 6C 65 44 20 C4 B5 BB D2 F2 CD

万一的 Delphi 博客
5B A2 53 5A 00 20 00 69 00 68 00 70 00 6C 00 65 00 44 00 20 76 84 4E 00 4E 07

万一的 Delphi 博客
CD BF A9 B2 20 69 68 70 6C 65 44 20 C4 B5 BB D2 F2 CD

万一的 Delphi 博客
5B A2 53 5A 00 20 00 69 00 68 00 70 00 6C 00 65 00 44 00 20 76 84 4E 00 4E 07
*)