delphi Byte 与 字符串(AnsiString、WideString) 相互转换

Byte 与 字符串(AnsiString、WideString) 相互转换

代码

String转Byte

procedure TForm1.Button1Click(Sender: TObject);
var
  buf: TBytes;
  I: Integer;
begin
  //ANSI编码
  buf := BytesOf('测试内容');
  Memo1.Lines.Add('ANSI编码');
  for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(buf[I].ToString);

  //Unicode编码
  buf := WideBytesOf('测试内容');
  Memo1.Lines.Add('Unicode编码');
  for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(buf[I].ToString);

  //Unicode编码,TEncoding.GetBytes方式
  buf := TEncoding.Unicode.GetBytes('测试内容');
  Memo1.Lines.Add('Unicode编码,GetBytes方式');
  for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(buf[I].ToString);
end;

D7中没有TEncoding,通过 Move 方法实现

procedure TForm1.Button1Click(Sender: TObject);
var
  buf: array of Byte;
  s1: AnsiString;
  s2: WideString;
  I: Integer;
begin
  //ANSI编码
  s1 := '测试内容';
  SetLength(buf, Length(s1));
  Move(s1[1], buf[0], Length(s1));
  Memo1.Lines.Add('ANSI编码');
  for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(IntToStr(buf[I]));
  
  //Unicode编码
  s2 := '测试内容';
  SetLength(buf, Length(s2) * SizeOf(WideChar));
  Move(s2[1], buf[0], Length(s2) * SizeOf(WideChar));
  Memo1.Lines.Add('Unicode编码');
  for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(IntToStr(buf[I]));
end;

D7中没有TEncoding,通过 CopyMemory 方法实现

procedure TForm1.Button2Click(Sender: TObject);
var
  buf: array of Byte;
  s1: AnsiString;
  p1: PAnsiChar;
  s2: WideString;
  p2: PWideChar;
  I: Integer;
begin
  //ANSI编码
  s1 := '测试内容';
  p1 := PAnsiChar(s1);
  SetLength(buf, Length(p1));
  CopyMemory(buf, p1, Length(p1));
  Memo1.Lines.Add('ANSI编码');
  for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(IntToStr(buf[I]));

  //Unicode编码
  s2 := '测试内容';
  p2 := PWideChar(s2);
  SetLength(buf, Length(p2) * SizeOf(WideChar));
  CopyMemory(buf, p2, Length(p2) * SizeOf(WideChar));
  Memo1.Lines.Add('Unicode编码');
  for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(IntToStr(buf[I]));
end;

Byte转String 编码格式

procedure TForm1.Button2Click(Sender: TObject);
var
  buf: TBytes;
begin
  //ANSI编码
  SetLength(buf, 4);
  buf[0] := 178;
  buf[1] := 226;
  buf[2] := 202;
  buf[3] := 212;
  Memo1.Lines.Add(StringOf(buf));

  //Unicode编码
  buf[0] := 75;
  buf[1] := 109;
  buf[2] := 213;
  buf[3] := 139;
  Memo1.Lines.Add(WideStringOf(buf));

  //Unicode编码,TEncoding.GetString方式
  Memo1.Lines.Add(TEncoding.Unicode.GetString(buf));
end;

D7中没有TEncoding,通过 Move 方法转 string 实现

var
  buf1: array [0..7] of Byte = (178, 226, 202, 212, 196, 218, 200, 221);
  buf2: array [0..7] of Byte = (75, 109, 213, 139, 133, 81, 185, 91);

procedure TForm1.Button3Click(Sender: TObject);
var
  s1: AnsiString;
  s2: WideString;
begin
  SetLength(s1, Length(buf1));
  Move(buf1[0], s1[1], Length(buf1));
  Memo1.Lines.Add(s1);

  SetLength(s2, Trunc(Length(buf2) / SizeOf(WideChar)));
  Move(buf2[0], s2[1], Length(buf2));
  Memo1.Lines.Add(s2);
end;

D7中没有TEncoding,通过 Move 方法转 PChar 实现

var
  buf1: array [0..7] of Byte = (178, 226, 202, 212, 196, 218, 200, 221);
  buf2: array [0..7] of Byte = (75, 109, 213, 139, 133, 81, 185, 91);

procedure TForm1.Button4Click(Sender: TObject);
var
  buf: array of Byte;
  s1: AnsiString;
  s2: WideString;
begin
  //ANSI编码
  //拷贝Byte数组,Length(buf1) + 1 为字符串最后的 #0
  SetLength(buf, Length(buf1) + 1);
  Move(buf1[0], buf[0], Length(buf1));
  s1 := PAnsiChar(@buf[0]);
  Memo1.Lines.Add(s1);

  //Unicode编码
  //拷贝Byte数组,Length(buf2) + 1 为字符串最后的 #0
  SetLength(buf, Length(buf2) + 1);
  Move(buf2[0], buf[0], Length(buf2));
  s2 := PWideChar(@buf[0]);
  Memo1.Lines.Add(s2);
end;

D7中没有TEncoding,通过 Char 组成实现

var
  buf1: array [0..7] of Byte = (178, 226, 202, 212, 196, 218, 200, 221);
  buf2: array [0..7] of Byte = (75, 109, 213, 139, 133, 81, 185, 91);

procedure TForm1.Button5Click(Sender: TObject);
var
  s1: AnsiString;
  s2: WideString;
  I: Integer;
begin
  for I := 0 to Length(buf1) - 1 do
  begin
    s1 := s1 + AnsiChar(buf1[I]);
  end;
  Memo1.Lines.Add(s1);

  for I := 0 to Length(buf2) - 1 do
  begin
    if Odd(I) then
      s2 := s2 + WideChar(MakeWord(buf2[I - 1], buf2[I]));
  end;
  Memo1.Lines.Add(s2);
end;

方法

System.SysUtils.StringOf

function StringOf(const Bytes: TBytes): UnicodeString;

将字节数组转换为 Unicode 字符串。 使用 TEncoding.Default 属性表示的默认系统区域设置进行转换。

System.SysUtils.WideStringOf

function WideStringOf(const Value: TBytes): UnicodeString;

将字节数组转换为 Unicode 字符串。使用 TEncoding.Unicode 属性表示的 Unicode 语言环境进行转换。

System.SysUtils.TEncoding.GetBytes

function GetBytes(Chars: PChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload;
function GetBytes(const Chars: Char): TBytes; overload;
function GetBytes(const Chars: array of Char): TBytes; overload;
function GetBytes(const Chars: TCharArray): TBytes; overload;
function GetBytes(const Chars: array of Char; CharIndex, CharCount: Integer): TBytes; overload;
function GetBytes(const Chars: TCharArray; CharIndex, CharCount: Integer): TBytes; overload;
function GetBytes(const Chars: array of Char; CharIndex, CharCount: Integer; const Bytes: TBytes; ByteIndex: Integer): Integer; overload;
function GetBytes(const Chars: TCharArray; CharIndex, CharCount: Integer; const Bytes: TBytes; ByteIndex: Integer): Integer; overload;
function GetBytes(const S: string): TBytes; overload;
function GetBytes(const S: string; CharIndex, CharCount: Integer; const Bytes: TBytes; ByteIndex: Integer): Integer; overload;
function GetBytes(const S: string; CharIndex, CharCount: Integer; const Bytes: TBytes; ByteIndex: Integer; const StringBaseIndex: Integer): Integer; overload;

将字符串或字符数组转换为字节数组。

参数

Chars 要编码的字符或字符数组 。

CharIndex 要编码的第一个字符的索引。

CharCount 指定要编码的字符数。

Bytes 结果字节数组。指定记录字节数组的位置。

ByteIndex 记录结果字节数组的索引。

S 要编码的字符串。

StringBaseIndex 字符串SCharIndex 的开始索引值。该值为 0 或 1。如果指定其他数字,则会引发 EEncodingError 异常。

返回值

编码后的字节数组或编码后的字节数。

System.SysUtils.TEncoding.GetString

function GetString(const Bytes: TBytes): string; overload; inline;
function GetString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): string; overload;
function GetString(const Bytes: array of Byte): string; overload;

将字节数组解码为字符串。

参数

Bytes 是要解码的字节数组。

ByteIndex 指定要解码的第一个字节。

ByteCount 指定要解码的字节数。

参考

字符串转byte数组

byte数组转字符串

WideCharToString

Unicode String Length in Bytes

Encoding.GetBytes 方法

posted @ 2024-02-28 19:08  txgh  阅读(252)  评论(0编辑  收藏  举报