var
 bb:array[0..15] of byte
 ss:string;
 nn,ii,nret:integer;
 
 nn:=288;
 ii:=258;
 zeroMemory(@bb[0],16);
 copyMemory(@bb[0],@nn,2);
 copyMemory(@bb[2],@ii,2); //288可以用2Byte存储(16bit),258也是,现在上面已经成功的存入bb中了,问题是怎么取出还原出来?其中bb中还不止存了这些,还有4B存的32bit整数,8B存的64bit整数,都要分段读还原出来。
 
type
  pint=^integer;
var
  pi:pint;
  pi:=bb
  nret:=pi^;//这不行的,我要分别将288和258还原出来,还有就是,就算我只存了288,这样取出来的也不是288,而是很大的一个整数。
  还有用makeword,makelong来实现也不行,好像还原后都变了,都是大大的一整数。请高手帮帮忙。

解决方案 »

  1.   

    TBytes=array of Bytefunction ToBytes(const AValue: Char): TBytes; overload;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.GetBytes(AValue);
      {$ELSE}
      SetLength(Result, SizeOf(Byte));
      Result[0] := Byte(AValue);
      {$ENDIF}
    end;function ToBytes(const AValue: Int64): TBytes; overload;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.GetBytes(AValue);
      {$ELSE}
      SetLength(Result, SizeOf(Int64));
      PInt64(@Result[0])^ := AValue;
      {$ENDIF}
    end;function ToBytes(const AValue: Integer): TBytes; overload;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.GetBytes(AValue);
      {$ELSE}
      SetLength(Result, SizeOf(Integer));
      PInteger(@Result[0])^ := AValue;
      {$ENDIF}
    end;function ToBytes(const AValue: Cardinal): TBytes; overload;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.GetBytes(AValue);
      {$ELSE}
      SetLength(Result, SizeOf(Cardinal));
      PCardinal(@Result[0])^ := AValue;
      {$ENDIF}
    end;function ToBytes(const AValue: Short): TBytes; overload;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.GetBytes(AValue);
      {$ELSE}
      SetLength(Result, SizeOf(Shortint));
      PShortint(@Result[0])^ := AValue;
      {$ENDIF}
    end;function ToBytes(const AValue: Word): TBytes; overload;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.GetBytes(AValue);
      {$ELSE}
      SetLength(Result, SizeOf(Word));
      PWord(@Result[0])^ := AValue;
      {$ENDIF}
    end;function ToBytes(const AValue: Byte): TBytes; overload;
    begin
      SetLength(Result, SizeOf(Byte));
      Result[0] := AValue;
    end;
    function BytesToChar(const AValue: TBytes): Char;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.ToChar(AValue, 0);
      {$ELSE}
      Result := Char(AValue[0]);
      {$ENDIF}
    end;function BytesToInteger(const AValue: TBytes): Integer;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.ToInt32(AValue, 0);
      {$ELSE}
      Result := PInteger(@AValue[0])^;
      {$ENDIF}
    end;function BytesToInt64(const AValue: TBytes): Int64;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.ToInt64(AValue,0);
      {$ELSE}
      Result := PInt64(@AValue[0])^;
      {$ENDIF}
    end;function BytesToWord(const AValue: TBytes): Word;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.ToUInt16(AValue,0);
      {$ELSE}
      Result := PWord(@AValue[0])^;
      {$ENDIF}
    end;function BytesToShort(const AValue: TBytes): Short;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.ToInt16(AValue,0);
      {$ELSE}
      Result := PShortint(@AValue[0])^;
      {$ENDIF}
    end;function BytesToCardinal(const AValue: TBytes): Cardinal;
    begin
      {$IFDEF DotNet}
      Result := System.BitConverter.ToUInt32(AValue, 0);
      {$ELSE}
      Result := PCardinal(@AValue[0])^;
      {$ENDIF}
    end;