作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Debian 7.0.0 上的 CodeBlocks 10.05 上使用 g++。
早在 90 年代,我编写了以下函数来反转 4 字节整数中的字节顺序。
/*******************************/
void ByteSwapInt(int *ipInteger)
/*******************************/
{
int iBuffer;
_swab( (char *)ipInteger, (char *)&iBuffer, 4 );
swaw((char *)&iBuffer, (char *)ipInteger, 4);
}
/*******************************/
void ByteSwapInt(int *ipInteger)
/*******************************/
{
int iBuffer;
int Int[4], Buf[4];
(Int[0]) = (*ipInteger >> 24) & 0xff; // high-order (leftmost) byte: bits 24-31
(Int[1]) = (*ipInteger >> 16) & 0xff; // next byte, counting from left: bits 16-23
(Int[2]) = (*ipInteger >> 8) & 0xff; // next byte, bits 8-15
(Int[3]) = *ipInteger & 0xff;
_swab( (char *)ipInteger, (char *)&iBuffer, 4 );
(Buf[0]) = (iBuffer >> 24) & 0xff; // high-order (leftmost) byte: bits 24-31
(Buf[1]) = (iBuffer >> 16) & 0xff; // next byte, counting from left: bits 16-23
(Buf[2]) = (iBuffer >> 8) & 0xff; // next byte, bits 8-15
(Buf[3]) = iBuffer & 0xff;
swaw((char *)&iBuffer, (char *)ipInteger, 4);
(Int[0]) = (*ipInteger >> 24) & 0xff; // high-order (leftmost) byte: bits 24-31
(Int[1]) = (*ipInteger >> 16) & 0xff; // next byte, counting from left: bits 16-23
(Int[2]) = (*ipInteger >> 8) & 0xff; // next byte, bits 8-15
(Int[3]) = *ipInteger & 0xff;
}
最佳答案
对于您想要的网络 htonl
, htons
和他们的同伴 ntohl
和 ntohs
,是 32 位和 16 位整数的主机到网络和网络到主机的转换。这些将针对您所在的架构进行适当定义。因此,在 SPARC 上,它们是空操作(大端平台),而在 x86 上,它们作为交换实现。他们来自 <arpa/inet.h>
或 <netinet/in.h>
关于c - 词交换。 swaw 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16908577/
我在 Debian 7.0.0 上的 CodeBlocks 10.05 上使用 g++。 早在 90 年代,我编写了以下函数来反转 4 字节整数中的字节顺序。 /*******************
我是一名优秀的程序员,十分优秀!