5,913
社区成员




另一个用PSmallInt
var
buf : array[0..1] of byte;
Pint:PSmallInt;
begin
buf[0] := $01;
buf[1] := $02;
Pint:=@buf[0];
ShowMessage(IntToStr( Pint^));
end;
类型转换
用个Integer指针直接指向字节数组就OK了,如果就想看看转换结果的话,MOVE都不用了
var
buf : array[0..3] of byte;
Pint:PInteger;
begin
buf[0] := $00;
buf[1] := $01;
buf[2] := $02;
buf[3] := $03;
Pint:=@buf[0];
ShowMessage(IntToStr( Pint^));
end;
buf: array[0..1] of byte;
bus: array[0..3] of byte;
i:SmallInt ;
j:Integer ;
begin
buf[0] := $01;
buf[1] := $02;
bus[0] := $00;
bus[1] := $01;
bus[2] := $02;
bus[3] := $03;
i := (I shl 16) or buf[1];
i := (I shl 8) or buf[0];
j := (J shl 32) or bus[3];
j := (J shl 24) or bus[2];
j := (J shl 16) or bus[1];
j := (J shl 8) or bus[0];
self.Memo1.Lines.Add(IntToStr(i));
self.Memo1.Lines.Add(IntToStr(j));
var
buf: array[0..1] of byte;
bus: array[0..3] of byte;
si:SmallInt ;
i:Integer ;
begin
buf[0] := $01;
buf[1] := $02;
bus[0] := $00;
bus[1] := $01;
bus[2] := $02;
bus[3] := $03;
Move(buf,si,SizeOf(SmallInt) );
ShowMessage(IntToStr(si));
Move(bus,i,SizeOf(Integer) );
ShowMessage(IntToStr(i));
var
buf : array[0..1] of byte;
bus : array[0..3] of byte;
function ByteToHex(InByte:byte):shortstring;
const Digits:array[0..15] of char='0123456789ABCDEF';
begin
result:=digits[InByte shr 4]+digits[InByte and $0F];
end;
function BinArrayToString(aArray: array of Byte): string;
var
i: integer;
begin
result:='';
for i:= Low(aArray) to High(aArray) do
begin
result:= result + ByteToHex(aArray[i]);
end;
Result:= IntToStr(StrToInt('$'+result));
end;
begin
buf[0] := $01;
buf[1] := $02;
showmessage(BinArrayToString(buf));
bus[0] := $00;
bus[1] := $01;
bus[2] := $02;
bus[3] := $03;
showmessage(BinArrayToString(bus));
end;