buf : array[0..1] of byte;buf[0] := $01;
buf[1] := $02;bus : array[0..3] of byte;bus[0] := $00;
bus[1] := $01;
bus[2] := $02;
bus[3] := $03;怎么把数组buf和bus转换为smallint和integer类型?多谢!!

解决方案 »

  1.   

    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;
      

  2.   

    //这个应该就可以了。
    var
      buf : array[0..1] of byte;
      bus : array[0..3] of byte;
      i:smallint;
      j:integer;
    begin
      buf[0] := $01;
      buf[1] := $02;
      move(buf[0],i,2);  bus[0] := $00;
      bus[1] := $01;
      bus[2] := $02;
      bus[3] := $03;
      move(bus[0],j,4);
    end;
      

  3.   


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

  4.   

      i:smallint; 
      j:integer;   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];(注意:根据大端或者小段做小相应的调整即可)
      

  5.   


    lz确定我们取出来地都是地址?你试了没有?  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));
      

  6.   


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

  7.   


    另一个用PSmallInt
    var
      buf : array[0..1] of byte;
      Pint:PSmallInt;
    begin
      buf[0] := $01;
      buf[1] := $02;  Pint:=@buf[0];
      ShowMessage(IntToStr( Pint^));
    end;
      

  8.   

    9楼:
    var
        buf : array[0..1] of byte;
        aBuf : Psmallint;
    begin
        buf[0] := $00;
        buf[1] := $14;
        aBuf := @buf[0];
        showMessage(intToStr(aBuf^));
    end;结果怎么是5120?