各种类型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;
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;
相关文章推荐
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- thinkphp5错误:类型错误: Argument 1 passed to think\Hook::import() must be of the type array
- how to set interface arrays : Illegal index into array of interfaces
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- ORA-04031: unable to allocate 4096 bytes of shared memory
- typeof + instanceof+toString+constructor是如何判断javascript数据类型的
- Move all zeroes to end of array
- php foreach 报 “Cannot create references to elements of a temporary array expression”
- The stack 'CSTACK' is filled to 100% (4096 bytes used out of 4096) 错误解决
- Minimum number of swaps required to sort an array
- System.Security.Cryptography.CryptographicException: The data to be decrypted exceeds the maximum for this modulus of 128 bytes. (RSACryptoServiceProv
- 为什么 ArrayList.toArray(T[]) 方法的参数类型是 T 而不是 E ?
- magento PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate xxxxx)
- How to use Jackson to deserialise an array of objects
- ORA-04031:unable to allocate N bytes of shared memory
- Oracle 12c 启用VARCHAR2, NVARCHAR2, and RAW类型支持32K(4,000 to 32,767 bytes)
- ORA-04031: unable to allocate 4096 bytes of shared memory ("shared pool","BEGIN :EXEC_STR := SYS.DBMS...","PL/SQL MPCODE","BAMIM
- Ruby: how to sort array of string parsing the content
- Pyspark系列笔记--错误Unable to acquire XXXXX bytes of memory