首页 > 其他分享 >delphi Byte 与 字符串(AnsiString、WideString) 相互转换

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

时间:2024-02-28 19:26:17浏览次数:15  
标签:AnsiString delphi Lines Add Length WideString Integer buf Memo1

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 方法

标签:AnsiString,delphi,Lines,Add,Length,WideString,Integer,buf,Memo1
From: https://www.cnblogs.com/txgh/p/18041485

相关文章

  • Delphi调用BPL并显示窗体
    bplunitUnit2;interfaceusesWinapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls;typeTForm2=class(TForm)Label1:TLabel;Button1:TBu......
  • delphi 提高unigui开发效率的两个方法
    1、编译时自己退出运行的程序。在做unigui开发时,每次编译运行时,unigui的应用都会在后台运行,每次重新编译时都必须手工在任务栏里将应用退出才行,非常麻烦,可以在项目编译的参数里加上杀进程的命令,这样每次重新编译时就会自动将旧的进程杀掉。命令参数如下:taskkill/f/im$(OUTPUT......
  • delphi Byte 与 AnsiChar、WideChar 相互转换
    Byte与AnsiChar、WideChar相互转换代码Byte转AnsiChar、WideCharprocedureTForm1.Button1Click(Sender:TObject);varac:AnsiChar;wc:WideChar;bys:TBytes;begin//ANSI编码ac:='a';bys:=BytesOf(ac);Memo1.Lines.Add(bys[0].ToString);......
  • delphi10.3农历日历控件安装使用
    控件下载,链接: https://pan.baidu.com/s/1-ZJFRMT2z5_kti0LX7bHCw提取码:2anv  Delphi10.3在File菜单下,选择”OPEN…”打开ChnCalendars.dpk文件    打开自带DemoD:\迅雷下载\ChnCalendar3.0Beta20......
  • delphi7农历日历控件安装使用。
    链接:https://pan.baidu.com/s/1-ZJFRMT2z5_kti0LX7bHCw提取码:2anv  Delphi7在File菜单下,选择”OPEN…”打开ChnCalendars.dpk文件    ......
  • delphi 每年、月、周、日的开始与结束的时间
    delphi每年、月、周、日的开始与结束的时间2022-07-1911:00:00网络Delphi1900 本文整理自网络,侵删。StartOfAYear...StartOfTheYear...EndOfAYear...EndOfTheYear...每年、月、周、日的开始与结束的时间{参数是指定的年、月、周、日}DateUtils.Start......
  • Delphi 实现刘谦春晚魔术
    试试看programProject1;{$APPTYPECONSOLE}{$R*.res}usesSystem.SysUtils,System.Classes;varlist,removeElement:TStringList;i,NameCount,insertIndex,directCount,sexCount:Integer;tmp,element,hiddenEle1,hiddenEle2:String;procedur......
  • Delphi 实现刘谦春晚魔术
    看了博友的C#实现刘谦春晚魔术很好,改成了delphi版的。 1programProject1;23{$APPTYPECONSOLE}4{$R*.res}56uses7System.SysUtils,8System.Classes;910var11list,removeElement:TStringList;12i,NameCount,insertI......
  • delphi按键值对重组字符串
    问题背景:有一组基金代码串,原逻辑按基金代码单个调整为按父子基金代码组作为条件获取查询结果解决原理:采用TStringList类哈希表操作方式重组字符串List:=TStringList.Create;List.Add('aaa=111');List.Add('bbb=222');List.Add('ccc=333');List.Add('ddd=444');ShowMessag......
  • delphi base64的“坑”
    delphibase64的“坑”使用Delphi原生的Base64编码(System.NetEncoding单元),编码后的字符串每隔76个字符会增加一个回车换行(#13#10),这样就导致和其他语言对接的时候出现问题,特别是Base64以后再进行签名,例如MD5签名,就会导致签名不一致。回车换行不容易看到,所以查找问题比较困难。......