您的位置:首页 > 其它

各种类型to array of bytes

2009-10-30 10:06 155 查看
原帖

http://topic.csdn.net/t/20051211/13/4451847.html

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐