delphi integer 内存存储与字节转换

一直不是很明白delphi中 integer 在内存中存储方式。经实验总结如下

==============================================================================================================

#  integer内存存储方式 :

    高字节在前,低字节在后。 如 $12 34 56 78  在内存中为 $78 56 34 12 ,即

    [3] = $78       120

    [2] = $56       86

    [1] = $34       52

    [0] = $12       18

# 此存储方式为主机字节存储,如需进行网络数据交互,需调用 htonl ,htons等函数进行转换

   转换后,可改变数据存储顺序。

==============================================================================================================

eg:

var
   iValue: Integer;
   aryBuf: array[ 0..3 ] of Byte;
begin
   iValue := $12345678;                                   //字节 $12 $34 $56 $78
   Move( iValue, aryBuf, SizeOf( Integer ) );      //aryBuf[0] = $12 aryBuf[1] = $34  aryBuf[2] = $56 aryBuf[3] = $78

   iValue := htonl( iValue );                              //主机字节与网络字段转换
   Move( iValue, aryBuf, SizeOf( Integer ) );      //aryBuf[0] = $78 aryBuf[1] = $56 aryBuf[2] = $34 aryBuf[3] = $12

end;

注:

htonl 函数使用应引用 WinSock单元


原文地址:https://www.cnblogs.com/nimorl/p/2045017.html