已知一个byte数组,
buf:array[0..1] of byte;
buf[0]:=$01;buf[1]:=$02;
将它转换为integer最后应该等于$0201=513
应该如何转换得到513?
Delphi byte数组如何转换integer
答案:3 悬赏:40 手机版
解决时间 2021-03-09 15:04
- 提问者网友:谁的错
- 2021-03-09 01:35
最佳答案
- 五星知识达人网友:躲不过心动
- 2021-03-09 02:50
{转换 TBytes 到 Integer}
procedure TForm1.Button1Click(Sender: TObject);
var
bs: TBytes; {TBytes 就是 Byte 的动态数组}
i: Integer;
begin
{它应该和 Integer 一样大小才适合转换}
SetLength(bs, 4);
bs[0] := $10;
bs[1] := $27;
bs[2] := 0;
bs[3] := 0;
{因为 TBytes 是动态数组, 所以它的变量 bs 是个指针; 所以先转换到 PInteger} i := PInteger(bs)^;
ShowMessage(IntToStr(i)); {10000}
end;
{从 Bytes 静态数组到 Integer 的转换会方便些}
procedure TForm1.Button2Click(Sender: TObject);
var
bs: array[0..3] of Byte;
i: Integer;
begin
bs[0] := $10;
bs[1] := $27;
bs[2] := 0;
bs[3] := 0;
i := Integer(bs);
ShowMessage(IntToStr(i)); {10000}
end;
{转换到自定义的结构}
procedure TForm1.Button3Click(Sender: TObject);
type
TData = packed record
a: Integer;
b: Word;
end;
var
bs: array[0..5] of Byte; {这个数组应该和结构大小一直}
data: TData;
begin
FillChar(bs, Length(bs), 0);
bs[0] := $10;
bs[1] := $27;
data := TData(bs);
ShowMessage(IntToStr(data.a)); {10000}
end;
{转换给自定义结构的一个成员}
procedure TForm1.Button4Click(Sender: TObject);
type
TData = packed record
a: Integer;
b: Word;
end;
var
bs: array[0..3] of Byte;
data: TData;
begin
FillChar(bs, Length(bs), 0);
bs[0] := $10;
bs[1] := $27;
data.a := Integer(bs);
ShowMessage(IntToStr(data.a)); {10000}
end;
procedure TForm1.Button1Click(Sender: TObject);
var
bs: TBytes; {TBytes 就是 Byte 的动态数组}
i: Integer;
begin
{它应该和 Integer 一样大小才适合转换}
SetLength(bs, 4);
bs[0] := $10;
bs[1] := $27;
bs[2] := 0;
bs[3] := 0;
{因为 TBytes 是动态数组, 所以它的变量 bs 是个指针; 所以先转换到 PInteger} i := PInteger(bs)^;
ShowMessage(IntToStr(i)); {10000}
end;
{从 Bytes 静态数组到 Integer 的转换会方便些}
procedure TForm1.Button2Click(Sender: TObject);
var
bs: array[0..3] of Byte;
i: Integer;
begin
bs[0] := $10;
bs[1] := $27;
bs[2] := 0;
bs[3] := 0;
i := Integer(bs);
ShowMessage(IntToStr(i)); {10000}
end;
{转换到自定义的结构}
procedure TForm1.Button3Click(Sender: TObject);
type
TData = packed record
a: Integer;
b: Word;
end;
var
bs: array[0..5] of Byte; {这个数组应该和结构大小一直}
data: TData;
begin
FillChar(bs, Length(bs), 0);
bs[0] := $10;
bs[1] := $27;
data := TData(bs);
ShowMessage(IntToStr(data.a)); {10000}
end;
{转换给自定义结构的一个成员}
procedure TForm1.Button4Click(Sender: TObject);
type
TData = packed record
a: Integer;
b: Word;
end;
var
bs: array[0..3] of Byte;
data: TData;
begin
FillChar(bs, Length(bs), 0);
bs[0] := $10;
bs[1] := $27;
data.a := Integer(bs);
ShowMessage(IntToStr(data.a)); {10000}
end;
全部回答
- 1楼网友:零点过十分
- 2021-03-09 04:49
// byte数组转换成string
function tfrmstringtobyte.bytetostring(const value: tbytearr): string;
var
i: integer;
s : string;
letra: char;
begin
s := '';
for i := length(value)-1 downto 0 do
begin
letra := chr(value[i] + 48);
s := letra + s;
end;
result := s;
end;
>> 运用eg:
showmessage(bytetostring(bytearray));
- 2楼网友:拜訪者
- 2021-03-09 04:28
(Integer(buf[1]) shl 8) or (buf[0])
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯