Delphi TBytes类型及与AnsiString、UnicodeString之间的转换

Delphi TBytes类型及与AnsiString、UnicodeString之间的转换

1、TBytes类型(引用单元:System.SysUtils)

1
2
3
type
  TArray<T> = array of T; 
  TBytes = TArray<Byte>;

故 TBytes 类型,可以看成是 array of Byte  

2、UnicodeString与TBytes的相互转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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, [ByteIndex]);
  if ByteCount < 0 then
    raise EEncodingError.CreateResFmt(@SInvalidCharCount, [ByteCount]);
  if (Length(Bytes) - ByteIndex) < ByteCount then
    raise EEncodingError.CreateResFmt(@SInvalidCharCount, [ByteCount]);
 
  Len := GetCharCount(Bytes, ByteIndex, ByteCount);
  if (ByteCount > 0) and (Len = 0) then
    raise EEncodingError.CreateRes(@SNoMappingForUnicodeCharacter);
  SetLength(Result, Len);
  GetChars(@Bytes[ByteIndex], ByteCount, PChar(Result), Len);
end;
 
function TEncoding.GetString(const Bytes: array of Byte): string;
var
  Len: Integer;
begin
  Len := GetCharCount(@Bytes[0], Length(Bytes));
  if (Length(Bytes) > 0) and (Len = 0) then
    raise EEncodingError.CreateRes(@SNoMappingForUnicodeCharacter);
  SetLength(Result, Len);
  GetChars(@Bytes[0], Length(Bytes), PChar(Result), Len);
end;

其他相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{$IFNDEF NEXTGEN}
function BytesOf(const Val: RawByteString): TBytes;
var
  Len: Integer;
begin
  Len := Length(Val);
  SetLength(Result, Len);
  Move(Val[1], Result[0], Len);
end;
 
function BytesOf(const Val: AnsiChar): TBytes;
begin
  SetLength(Result, 1);
  Result[0] := Byte(Val);
end;
 
function BytesOf(const Val: WideChar): TBytes;
begin
  Result := BytesOf(UnicodeString(Val));
end;
{$ENDIF}

3、AnsiString 与 TBytes 的相互转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function BytesOf(const Val: AnsiString): TBytes;    
var
  Len: Integer;
begin
  Len := Length(Val);
  SetLength(Result, Len);
  Move(Val[1], Result[0], Len);
end;
 
function StringOf(const buf:TBytes): AnsiString;
begin
  SetLength(Result, Length(buf));
  CopyMemory(PAnsiChar(result), @buf[0], Length(buf));
end;

  

 

 

 

创建时间:2022.03.14  更新时间:

posted on   滔Roy  阅读(1947)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本

导航

点击右上角即可分享
微信分享提示