星辰梦幻 發表於 2024-2-28 19:08:00

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

<h1 id="byte-与-字符串ansistringwidestring-相互转换">Byte 与 字符串(AnsiString、WideString) 相互转换</h1>
<h2 id="代码">代码</h2>
<h3 id="string转byte">String转Byte</h3>
<pre><code class="language-delphi">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.ToString);

//Unicode编码
buf := WideBytesOf('测试内容');
Memo1.Lines.Add('Unicode编码');
for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(buf.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.ToString);
end;
</code></pre>
<p>D7中没有<code>TEncoding</code>,通过 <code>Move</code> 方法实现</p>
<pre><code class="language-delphi">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, buf, Length(s1));
Memo1.Lines.Add('ANSI编码');
for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(IntToStr(buf));

//Unicode编码
s2 := '测试内容';
SetLength(buf, Length(s2) * SizeOf(WideChar));
Move(s2, buf, Length(s2) * SizeOf(WideChar));
Memo1.Lines.Add('Unicode编码');
for I := 0 to Length(buf) - 1 do
    Memo1.Lines.Add(IntToStr(buf));
end;
</code></pre>
<p>D7中没有<code>TEncoding</code>,通过 <code>CopyMemory</code> 方法实现</p>
<pre><code class="language-delphi">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));

//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));
end;
</code></pre>
<h3 id="byte转string--编码格式">Byte转String编码格式</h3>
<pre><code class="language-delphi">procedure TForm1.Button2Click(Sender: TObject);
var
buf: TBytes;
begin
//ANSI编码
SetLength(buf, 4);
buf := 178;
buf := 226;
buf := 202;
buf := 212;
Memo1.Lines.Add(StringOf(buf));

//Unicode编码
buf := 75;
buf := 109;
buf := 213;
buf := 139;
Memo1.Lines.Add(WideStringOf(buf));

//Unicode编码,TEncoding.GetString方式
Memo1.Lines.Add(TEncoding.Unicode.GetString(buf));
end;
</code></pre>
<p>D7中没有<code>TEncoding</code>,通过 <code>Move</code> 方法转 <code>string</code> 实现</p>
<pre><code class="language-delphi">var
buf1: array of Byte = (178, 226, 202, 212, 196, 218, 200, 221);
buf2: array 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, s1, Length(buf1));
Memo1.Lines.Add(s1);

SetLength(s2, Trunc(Length(buf2) / SizeOf(WideChar)));
Move(buf2, s2, Length(buf2));
Memo1.Lines.Add(s2);
end;
</code></pre>
<p>D7中没有<code>TEncoding</code>,通过 <code>Move</code> 方法转 <code>PChar</code> 实现</p>
<pre><code class="language-delphi">var
buf1: array of Byte = (178, 226, 202, 212, 196, 218, 200, 221);
buf2: array 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, buf, Length(buf1));
s1 := PAnsiChar(@buf);
Memo1.Lines.Add(s1);

//Unicode编码
//拷贝Byte数组,Length(buf2) + 1 为字符串最后的 #0
SetLength(buf, Length(buf2) + 1);
Move(buf2, buf, Length(buf2));
s2 := PWideChar(@buf);
Memo1.Lines.Add(s2);
end;
</code></pre>
<p>D7中没有<code>TEncoding</code>,通过 <code>Char</code> 组成实现</p>
<pre><code class="language-delphi">var
buf1: array of Byte = (178, 226, 202, 212, 196, 218, 200, 221);
buf2: array 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);
end;
Memo1.Lines.Add(s1);

for I := 0 to Length(buf2) - 1 do
begin
    if Odd(I) then
      s2 := s2 + WideChar(MakeWord(buf2, buf2));
end;
Memo1.Lines.Add(s2);
end;
</code></pre>
<h2 id="方法">方法</h2>
<h3 id="systemsysutilsstringof">System.SysUtils.StringOf</h3>
<pre><code class="language-delphi">function StringOf(const Bytes: TBytes): UnicodeString;
</code></pre>
<p>将字节数组转换为 Unicode 字符串。 使用 <code>TEncoding.Default</code> 属性表示的默认系统区域设置进行转换。</p>
<h3 id="systemsysutilswidestringof">System.SysUtils.WideStringOf</h3>
<pre><code class="language-delphi">function WideStringOf(const Value: TBytes): UnicodeString;
</code></pre>
<p>将字节数组转换为 Unicode 字符串。使用 <code>TEncoding.Unicode</code> 属性表示的 Unicode 语言环境进行转换。</p>
<h3 id="systemsysutilstencodinggetbytes">System.SysUtils.TEncoding.GetBytes</h3>
<pre><code class="language-delphi">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;
</code></pre>
<p>将字符串或字符数组转换为字节数组。</p>
<p><strong>参数</strong></p>
<p><em>Chars</em>要编码的字符或字符数组 。</p>
<p><em>CharIndex</em>要编码的第一个字符的索引。</p>
<p><em>CharCount</em>指定要编码的字符数。</p>
<p><em>Bytes</em>结果字节数组。指定记录字节数组的位置。</p>
<p><em>ByteIndex</em>记录结果字节数组的索引。</p>
<p><em>S</em>要编码的字符串。</p>
<p><em>StringBaseIndex</em>字符串<em>S</em>上 <em>CharIndex</em> 的开始索引值。该值为 0 或 1。如果指定其他数字,则会引发 <code>EEncodingError</code> 异常。</p>
<p><strong>返回值</strong></p>
<p>编码后的字节数组或编码后的字节数。</p>
<h3 id="systemsysutilstencodinggetstring">System.SysUtils.TEncoding.GetString</h3>
<pre><code class="language-delphi">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;
</code></pre>
<p>将字节数组解码为字符串。</p>
<p><strong>参数</strong></p>
<p><em>Bytes</em>是要解码的字节数组。</p>
<p><em>ByteIndex</em>指定要解码的第一个字节。</p>
<p><em>ByteCount</em>指定要解码的字节数。</p>
<h2 id="参考">参考</h2>
<p>字符串转byte数组</p>
<p>byte数组转字符串</p>
<p>WideCharToString</p>
<p>Unicode String Length in Bytes</p>
<p>Encoding.GetBytes 方法</p><br><br>
来源:https://www.cnblogs.com/txgh/p/18041485
頁: [1]
查看完整版本: delphi Byte 与 字符串(AnsiString、WideString) 相互转换