可靠的 WinSock API 确定我的机器上使用的字节序
Reliable WinSock API that determines which endian is used on my machine
一开始我想用ntohs,但是备注中的以下部分迫使我在这里寻求帮助:
If the netshort parameter is already in host byte order, then this
function will reverse it. It is up to the application to determine if
the byte order must be reversed.
我可以尝试这样的事情:
short int i= 0x1;
char *c= (char*)&i;
if(c[0] == 1) // little endian, use ntohs
但我想知道是否有 Winsock API 函数可以做同样的事情?
虽然我没有测试,WSANtohs 函数没有包含让应用程序检查是否必须颠倒字节顺序的注释。它确实说它检查网络字节顺序:
The socket passed in the s parameter is used to determine the network
byte order required based on the Winsock catalog protocol entry
associated with the socket.
一开始我想用ntohs,但是备注中的以下部分迫使我在这里寻求帮助:
If the netshort parameter is already in host byte order, then this function will reverse it. It is up to the application to determine if the byte order must be reversed.
我可以尝试这样的事情:
short int i= 0x1;
char *c= (char*)&i;
if(c[0] == 1) // little endian, use ntohs
但我想知道是否有 Winsock API 函数可以做同样的事情?
虽然我没有测试,WSANtohs 函数没有包含让应用程序检查是否必须颠倒字节顺序的注释。它确实说它检查网络字节顺序:
The socket passed in the s parameter is used to determine the network byte order required based on the Winsock catalog protocol entry associated with the socket.