gpt4 book ai didi

c - 词交换。 swaw 已弃用

转载 作者:行者123 更新时间:2023-12-04 04:54:46 26 4
gpt4 key购买 nike

我在 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);
}

直到最近,它才奏效。但我注意到 swaw 似乎不再做任何事情。我通过扩展上述函数,在 *ipInteger 和 iBuffer 中创建单个字节的数组来检查发生了什么
/*******************************/
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;
}

*ipInteger 的内容不会改变。我试图在谷歌上找到 swaw,用于交换单词,但没有成功。它被弃用了吗?

最佳答案

对于您想要的网络 htonl , htons和他们的同伴 ntohlntohs ,是 32 位和 16 位整数的主机到网络和网络到主机的转换。这些将针对您所在的架构进行适当定义。因此,在 SPARC 上,它们是空操作(大端平台),而在 x86 上,它们作为交换实现。他们来自 <arpa/inet.h><netinet/in.h>

关于c - 词交换。 swaw 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16908577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com