htonl

How to set sockaddr_in6::sin6_addr byte order to network byte order?

半腔热情 提交于 2019-11-29 13:18:35
问题 I developing a network application and using socket APIs. I want to set sin6_addr byte order of sockaddr_in6 structure. For 16 bits or 32 bits variables, it is simple: Using htons or htonl: // IPv4 sockaddr_in addr; addr.sin_port = htons(123); addr.sin_addr.s_addr = htonl(123456); But for 128 bits variables, I dont know how to set byte order to network byte order: // IPv6 sockaddr_in6 addr; addr.sin6_port = htons(123); addr.sin6_addr.s6_addr = ??? // 16 bytes with network byte order but how

ntohs() and ntohl() equivalent?

有些话、适合烂在心里 提交于 2019-11-29 09:03:55
Are there net to host conversion functions in C#? Googling and not finding much. :P IPAddress.HostToNetworkOrder and IPAddress.NetworkToHostOrder ? Each method has overloads for 16, 32 and 64 bit integers. caligari @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 BitConverter class (static class in the core assembly) that brings you a more suitable way. Specially

Does cast between signed and unsigned int maintain exact bit pattern of variable in memory?

雨燕双飞 提交于 2019-11-28 17:15:21
I want to pass a 32-bit signed integer x through a socket. In order that the receiver knows which byte order to expect, I am calling htonl(x) before sending. htonl expects a uint32_t though and I want to be sure of what happens when I cast my int32_t to a uint32_t . int32_t x = something; uint32_t u = (uint32_t) x; Is it always the case that the bytes in x and u each will be exactly the same? What about casting back: uint32_t u = something; int32_t x = (int32_t) u; I realise that negative values cast to large unsigned values but that doesn't matter since I'm just casting back on the other end.

Does cast between signed and unsigned int maintain exact bit pattern of variable in memory?

爷,独闯天下 提交于 2019-11-27 10:20:21
问题 I want to pass a 32-bit signed integer x through a socket. In order that the receiver knows which byte order to expect, I am calling htonl(x) before sending. htonl expects a uint32_t though and I want to be sure of what happens when I cast my int32_t to a uint32_t . int32_t x = something; uint32_t u = (uint32_t) x; Is it always the case that the bytes in x and u each will be exactly the same? What about casting back: uint32_t u = something; int32_t x = (int32_t) u; I realise that negative

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

独自空忆成欢 提交于 2019-11-26 19:48:38
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 but I don't know how to check the endianness at compile-time to make the code portable. So your help is