-1

Delphi 中是否有等效的 bitconverter.getbytes?

https://msdn.microsoft.com/en-us/library/fk3sts66(v=vs.110).aspx

4

2 回答 2

1

您可以使用简单的演员表。使用与 Remy 相同的类型,如下所示:

var
  value: Smallint;
  arr: array[0..SizeOf(Smallint)-1] of Byte;
begin
  value := ...; 
  // by value cast:
  Smallint(arr) := value;
  // or by address cast:
  PSmallint(@arr)^ := value;
end;

或这个:

var
  value: Smallint;
  arr: TBytes;
begin
  value := ...; 
  SetLength(arr, SizeOf(value));
  PSmallint(arr)^ := value;
end;
于 2015-11-12T07:41:53.633 回答
0

只需Move()将输入变量转换为合适大小的字节数组,例如:

var
  value: Smallint;
  arr: array[0..SizeOf(Smallint)-1] of Byte;
begin
  value := ...; 
  Move(value, arr[0], SizeOf(value));
end;

或者:

type
  TBytes = array of Byte;

function GetBytes(value: Smallint): TBytes;
begin
  SetLength(Result, SizeOf(value));
  Move(value, Result[0], SizeOf(value));
end;

var
  value: Smallint;
  arr: TBytes;
begin
  value := ...; 
  arr := GetBytes(value);
end;
于 2015-11-11T20:05:32.860 回答