delphi Byte 与 AnsiChar、WideChar 相互转换

Byte 与 AnsiChar、WideChar 相互转换

代码

AnsiChar、WideChar转Byte

procedure TForm1.Button1Click(Sender: TObject);
var
  ac: AnsiChar;
  wc: WideChar;
  bys: TBytes;
begin
  //ANSI编码
  ac := 'a';
  bys := BytesOf(ac);
  Memo1.Lines.Add(bys[0].ToString);
  //Unicode编码
  wc := 'a';
  bys := WideBytesOf(wc);
  Memo1.Lines.Add(bys[0].ToString);
  Memo1.Lines.Add(bys[1].ToString);
end;

D7中没有TEncoding

procedure TForm1.Button1Click(Sender: TObject);
var
  ac: AnsiChar;
  wc: WideChar;
  b1, b2: Byte;
begin
  //ANSI编码
  ac := 'a';
  b1 := Byte(ac);
  Memo1.Lines.Add(IntToStr(b1));
  //Unicode编码
  wc := 'a';
  b1 := WordRec(wc).Lo;
  b2 := WordRec(wc).Hi;
  Memo1.Lines.Add(IntToStr(b1));
  Memo1.Lines.Add(IntToStr(b2));
end;

Byte转AnsiChar、WideChar

procedure TForm1.Button2Click(Sender: TObject);
var
  bys: TBytes;
  chars: TCharArray;
begin
  //ANSI编码
  SetLength(bys, 1);
  bys[0] := 97;
  chars := TEncoding.ANSI.GetChars(bys);
  Memo1.Lines.Add(chars[0]);
  //Unicode编码
  SetLength(bys, 2);
  bys[0] := 97;
  bys[1] := 0;
  Memo1.Lines.Add(chars[0]);
end;

D7中没有TEncoding

procedure TForm1.Button2Click(Sender: TObject);
var
  ac: AnsiChar;
  wc: WideChar;
  b1, b2: Byte;
begin
  //ANSI编码
  b1 := 97;
  ac := AnsiChar(b1);
  Memo1.Lines.Add(ac);
  //Unicode编码
  b1 := 97;
  b2 := 0;
  wc := WideChar(MakeWord(b1, b2));
  Memo1.Lines.Add(wc);
end;

方法

System.SysUtils.BytesOf

function BytesOf(const Val: RawByteString): TBytes;
function BytesOf(const Val: WideChar): TBytes;
function BytesOf(const Val: AnsiChar): TBytes; overload;
function BytesOf(const Val: UnicodeString): TBytes;
function BytesOf(const Val: Pointer; const Len: Integer): TBytes;

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

重载

BytesOf(UnicodeString )BytesOf(WideString ) 使用 TEncoding.Default 属性表示的默认系统区域设置进行转换。

System.SysUtils.WideBytesOf

function WideBytesOf(const Value: UnicodeString): TBytes;

将 Unicode 字符串转换为字节数组。使用 TEncoding.Unicode 属性表示的 UTF-16 编码进行转换。

BytesOf 方法不同,生成的字节数组包含输入字符串中每个字符的两个字节。

System.SysUtils.TEncoding.GetChars

function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PChar; CharCount: Integer): Integer; overload;
function GetChars(const Bytes: array of Byte): TCharArray; overload;
function GetChars(const Bytes: TBytes): TCharArray; overload;
function GetChars(const Bytes: array of Byte; ByteIndex, ByteCount: Integer): TCharArray; overload;
function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer): TCharArray; overload;
function GetChars(const Bytes: array of Byte; ByteIndex, ByteCount: Integer;
const Chars: array of Char; CharIndex: Integer): Integer; overload;
function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer;
const Chars: TCharArray; CharIndex: Integer): Integer; overload;

将一组字节解码为一组字符。

参数

Bytes 可以是字节指针或字节数组。

ByteIndex 指定要解码的第一个字节。如果不存在,则索引为 0。

ByteCount 指定要解码的字节数。如果不存在,则所有字节都将被解码。

Chars 指向解码字符目的地的指针。

CharIndex 指定 Chars 中开始写入结果的位置。

返回值

解码后的字符数组或解码后的字符数

System.SysUtils.WordRec

WordRec = packed record

将指定变量的高位和低位字节存储为 Byte 类型。主要用于类型转换。

参考

WideChar to Bytes

Combine two Bytes to WideChar

posted @ 2024-02-24 11:16  txgh  阅读(71)  评论(0编辑  收藏  举报