Delphi byte[]转换为int


http://bbs.csdn.net/topics/330082331 

1、

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));

2、

类型转换
用个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;

3、

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;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM