htonl

大小端模式转换函数

一个人想着一个人 提交于 2019-12-24 21:38:10
htonl(),htons(),ntohl(),ntons()--大小端模式转换函数 不同机器内部对变量的字节存储顺序不同,有的采用大端模式(big-endian),有的采用小端模式(little-endian)。 大端模式是指高字节数据存放在低地址处,低字节数据放在高地址处。 小端模式是指低字节数据存放在低地址处,高字节数据放在高地址处。 在网络上传输数据时,由于数据传输的两端可能对应不同的硬件平台,采用的存储字节顺序也可能不一致,因此 TCP/IP 协议规定了在网络上必须采用网络字节顺序(也就是大端模式) 。 通过对大小端的存储原理分析可发现,对于 char 型数据,由于其只占一个字节,所以不存在这个问题,这也是一般情况下把数据缓冲区定义成 char 类型 的原因之一。对于 IP 地址、端口号等非 char 型数据,必须在数据发送到网络上之前将其转换成大端模式,在接收到数据之后再将其转换成符合接收端主机的存储模式。 Linux 系统为大小端模式的转换提供了 4 个函数,输入 man byteorder 命令可得函数原型: #include <arpa/inet.h> uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong);

ntohs() and ntohl() equivalent?

喜你入骨 提交于 2019-12-18 05:27:15
问题 Are there net to host conversion functions in C#? Googling and not finding much. :P 回答1: IPAddress.HostToNetworkOrder and IPAddress.NetworkToHostOrder? Each method has overloads for 16, 32 and 64 bit integers. 回答2: @jon-skeet's answer is the most accurate according to your question. However, 'ntoh_' and 'hton_' C functions are extensively used in order to translate between little-endian and big-endian computer architectures. If your intention is to perform endianess conversions , there is a

Is there any “standard” htonl-like function for 64 bits integers in C++?

柔情痞子 提交于 2019-12-17 04:23:14
问题 I'm working on an implementation of the memcache protocol which, at some points, uses 64 bits integer values. These values must be stored in "network byte order". I wish there was some uint64_t htonll(uint64_t value) function to do the change, but unfortunately, if it exist, I couldn't find it. So I have 1 or 2 questions: Is there any portable (Windows, Linux, AIX) standard function to do this ? If there is no such function, how would you implement it ? I have in mind a basic implementation

Is there any “standard” htonl-like function for 64 bits integers in C++?

泄露秘密 提交于 2019-12-17 04:22:06
问题 I'm working on an implementation of the memcache protocol which, at some points, uses 64 bits integer values. These values must be stored in "network byte order". I wish there was some uint64_t htonll(uint64_t value) function to do the change, but unfortunately, if it exist, I couldn't find it. So I have 1 or 2 questions: Is there any portable (Windows, Linux, AIX) standard function to do this ? If there is no such function, how would you implement it ? I have in mind a basic implementation

Using ntohl and htonl problems on iPhone

*爱你&永不变心* 提交于 2019-12-14 03:02:09
问题 I have an NSDATA object which I create and then send over the network. I have having trouble getting the correct values out of the received NSDATA stream. Here is some quick code to reproduce my problem. No network transmission required. NSMutableData *data = [[NSMutableData alloc] initWithCapacity:150]; // put data in the array [data woaAppendInt8:4]; [data woaAppendInt32:2525]; [data woaAppendInt8:6]; [data woaAppendInt32:1616]; // get data out of array size_t offset = 0; int x1 = [data

ntohl() vs htonl() in Little Endian

佐手、 提交于 2019-12-12 17:06:02
问题 Kindly clarify my doubt as i got so confused with the below stuff and i couldnt get a clean anwser any where else on net. #include<stdio.h> int main() { int a = 0x44332211; printf("Network - 0x%x\n", htonl(a));// Host to network printf("Host - 0x%x\n", ntohl(a));// Network to host return 0; } Output: Network - 0x11223344 Host - 0x11223344 Here htonl(0x44332211) => i am converting little endian(LE) to BE. So output will be 0x11223344 . That i understood. My problem is with ntoh() . Now ntohl

Cross-platform definition of _byteswap_uint64 and _byteswap_ulong

﹥>﹥吖頭↗ 提交于 2019-12-12 15:08:41
问题 Visual Studio defines _byteswap_uint64 and _byteswap_ulong in stdlib.h. Am I right to assume, that this is not standard and won't compile on Linux or Darwin? Is there a way to define these includes in a cross-platform way? 回答1: Google's CityHash source code uses this code: https://github.com/google/cityhash/blob/8af9b8c2b889d80c22d6bc26ba0df1afb79a30db/src/city.cc#L50 #ifdef _MSC_VER #include <stdlib.h> #define bswap_32(x) _byteswap_ulong(x) #define bswap_64(x) _byteswap_uint64(x) #elif

Portable C binary serialization primitives

一个人想着一个人 提交于 2019-12-12 07:49:37
问题 As far as I know, the C library provides no help in serializing numeric values into a non-text byte stream. Correct me if I'm wrong. The most standard tool in use is htonl et al from POSIX. These functions have shortcomings: There is no 64-bit support. There is no floating-point support. There are no versions for signed types. When deserializing, the unsigned-to-signed conversion relies on signed integral overflow which is UB. Their names do not state the size of the datatype. They depend on

use htonl convert a int number, and memcpy to a char*, but nothing

一笑奈何 提交于 2019-12-11 13:55:28
问题 code like this: int totalLen = 50; int usTest = htons(totalLen); char* strBuf = new char[totalLen ]; memcpy(strBuf,&usTest,sizeof(int)); after this,there is nothing in strBuf, why? but if I put a big number, like 100000001 , it will be OK? what's the problem? 来源: https://stackoverflow.com/questions/19818704/use-htonl-convert-a-int-number-and-memcpy-to-a-char-but-nothing

c++ server htons to java ntohs client conversion

自作多情 提交于 2019-12-10 20:08:51
问题 I am creating a small TFTP client server app where server is developed using c++ and client using java. Here i am sending "block count" value using htons conversion. But i am not able to convert it back to its original value at client. For example if am sending block count ntohs(01) (2 bytes) from server to client. Client is reading in bytes. Value which I am receiving is byte 0 and byte 1. Please if any one can provide a solution. 回答1: I take it you meant that you use ntohs to decode the