byte数组转换成integer

bphantom 2005-12-11 01:33:48
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来实现也不行,好像还原后都变了,都是大大的一整数。

请高手帮帮忙。
...全文
712 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bphantom 2005-12-11
  • 打赏
  • 举报
回复
mastersky 2005-12-11
  • 打赏
  • 举报
回复
不错。简单有效的方法。
madyak 2005-12-11
  • 打赏
  • 举报
回复
TBytes=array of Byte

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

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧