Delphi TBytes类型及与AnsiString、UnicodeString之间的转换
<p><span style="font-size: 16px"><strong>Delphi TBytes类型及与AnsiString、UnicodeString之间的转换</strong></span></p><p><span style="font-size: 16px"><strong>1、TBytes类型(引用单元:System.SysUtils)</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">type
TArray<T> = array of T;
TBytes = TArray<Byte>;
</span></pre>
</div>
<p><span style="font-size: 16px">故 TBytes 类型,可以看成是 array of Byte </span></p>
<p><span style="font-size: 16px"><strong>2、UnicodeString与TBytes的相互转换</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">function TEncoding.GetBytes(const S: string): TBytes;
var
Len: Integer;
begin
Len := GetByteCount(S);
SetLength(Result, Len);
GetBytes(S, Low(S), Length(S), Result, 0, Low(S));
end;
function BytesOf(const Val: UnicodeString): TBytes;
begin
Result := TEncoding.Default.GetBytes(Val);
end;
function StringOf(const Bytes: TBytes): UnicodeString;
begin
if Assigned(Bytes) then
Result := TEncoding.Default.GetString(Bytes, Low(Bytes), High(Bytes) + 1)
else
Result := '';
end;
function TEncoding.GetString(const Bytes: TBytes): string;
begin
Result := GetString(Bytes, 0, Length(Bytes));
end;
function TEncoding.GetString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): string;
var
Len: Integer;
begin
if (Length(Bytes) = 0) and (ByteCount <> 0) then
raise EEncodingError.CreateRes(@SInvalidSourceArray);
if ByteIndex < 0 then
raise EEncodingError.CreateResFmt(@SByteIndexOutOfBounds, );
if ByteCount < 0 then
raise EEncodingError.CreateResFmt(@SInvalidCharCount, );
if (Length(Bytes) - ByteIndex) < ByteCount then
raise EEncodingError.CreateResFmt(@SInvalidCharCount, );
Len := GetCharCount(Bytes, ByteIndex, ByteCount);
if (ByteCount > 0) and (Len = 0) then
raise EEncodingError.CreateRes(@SNoMappingForUnicodeCharacter);
SetLength(Result, Len);
GetChars(@Bytes, ByteCount, PChar(Result), Len);
end;
function TEncoding.GetString(const Bytes: array of Byte): string;
var
Len: Integer;
begin
Len := GetCharCount(@Bytes, Length(Bytes));
if (Length(Bytes) > 0) and (Len = 0) then
raise EEncodingError.CreateRes(@SNoMappingForUnicodeCharacter);
SetLength(Result, Len);
GetChars(@Bytes, Length(Bytes), PChar(Result), Len);
end;
</span></pre>
</div>
<p><span style="font-size: 16px">其他相关</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">{$IFNDEF NEXTGEN}
function BytesOf(const Val: RawByteString): TBytes;
var
Len: Integer;
begin
Len := Length(Val);
SetLength(Result, Len);
Move(Val, Result, Len);
end;
function BytesOf(const Val: AnsiChar): TBytes;
begin
SetLength(Result, 1);
Result := Byte(Val);
end;
function BytesOf(const Val: WideChar): TBytes;
begin
Result := BytesOf(UnicodeString(Val));
end;
{$ENDIF}</span></pre>
</div>
<p><span style="font-size: 16px"><strong>3、AnsiString 与 TBytes 的相互转换</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">function BytesOf(const Val: AnsiString): TBytes;
var
Len: Integer;
begin
Len := Length(Val);
SetLength(Result, Len);
Move(Val, Result, Len);
end;
function StringOf(const buf:TBytes): AnsiString;
begin
SetLength(Result, Length(buf));
CopyMemory(PAnsiChar(result), @buf, Length(buf));
end;
</span></pre>
</div>
<p><span style="font-size: 16px"> </span></p>
<p> </p>
<p> </p>
<p> </p>
<p><span style="color: rgba(136, 136, 136, 1)">创建时间:2022.03.14 更新时间:</span></p>
</div>
<div id="MySignature" role="contentinfo">
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/16004081.html
頁:
[1]