Questions tagged [htonl]

htonl function converts a u_long from host to TCP/IP network byte order (which is big-endian).

Shortcut for Host TO Network translation

The htonl function can be used to convert an IPv4 address in host byte order to the IPv4 address in network byte order.

Docs: https://msdn.microsoft.com/en-us/library/windows/desktop/ms738556(v=vs.85).aspx

Answer: When and how to use C++ htonl function

42 questions
69
votes
7 answers

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

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…
ereOn
  • 48,328
  • 33
  • 147
  • 228
36
votes
3 answers

ntohs() and ntohl() equivalent?

Are there net to host conversion functions in C#? Googling and not finding much. :P
bobber205
  • 11,636
  • 25
  • 71
  • 97
29
votes
3 answers

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

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…
Flash
  • 13,804
  • 11
  • 65
  • 91
10
votes
2 answers

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

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 =…
Amir Saniyan
  • 10,990
  • 18
  • 77
  • 124
10
votes
3 answers

Portable C binary serialization primitives

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…
Potatoswatter
  • 126,977
  • 21
  • 238
  • 404
9
votes
3 answers

Confusion in htons- little endian/ big endian

When I send a integer variable from one process to other through socket, and then printing the value at received end, the value is still the same without using ntohl/htonl, then where do I need to use these functions other than initializing socket…
avd
  • 12,513
  • 29
  • 74
  • 96
5
votes
3 answers

host to network double?

I'd like to send some double precision floating point numbers over the network. (standard C, standard sockets) There is no htond or ntohd to convert the data to and from network byte order. What should I do? I have a couple solutions in my head but…
dec-vt100
  • 190
  • 2
  • 9
5
votes
2 answers

Cross-platform definition of _byteswap_uint64 and _byteswap_ulong

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?
ARF
  • 6,320
  • 5
  • 34
  • 62
5
votes
1 answer

When and how to use C++ htonl function

cout << "Hello World !" << endl; For my very first post on stackoverflow: When are we supposed to use the htonl function? I have gone through the man page. However, I don't really understand when and how to use it.
json27
  • 69
  • 1
  • 1
  • 5
4
votes
2 answers

Initializing a const variable with ntohl()

I'm trying to initialize a global-scoped const variable with a value that is byte-swapped appropriately. #include #include #include const uint32_t a = ntohl(0x11223344); int main(int argc, char const *argv[]) { …
Steve King
  • 43
  • 3
4
votes
2 answers

ntohl() vs htonl() in Little Endian

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 int main() { int a = 0x44332211; printf("Network - 0x%x\n", htonl(a));// Host to network …
Subi Suresh
  • 273
  • 1
  • 3
  • 9
3
votes
1 answer

TCP socket: When ntoh/hton conversion not needed?

I am using existing code, that passes data - union ibv_gid through TCP connection without converting the endianness. There is a comment inside: "The gid will be transfer byte by byte so no need to do "hton". The code is correct and works, but I…
Halona
  • 1,401
  • 1
  • 11
  • 19
3
votes
1 answer

How to convert __u32 to __be32 in Linux Kernel

I've a variable __be32 x; I've a function __u32 foo(void){ __u32 a; return a; } I need to store the return of foo in variable x. x=htonl(foo()); Is it correct? I've a confusion that what are the return types of ntohl() and htonl().…
RatDon
  • 3,071
  • 6
  • 33
  • 73
3
votes
3 answers

c++ server htons to java ntohs client conversion

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…
changed
  • 1,913
  • 7
  • 33
  • 53
3
votes
5 answers

Is network byte order pointless under IPv6?

If we use a 32-bit integer to store an IPv4 address, then the byte order of the integer must be considered. However, as there is no built-in 128-bit integer type under almost all platforms, an IPv6 address must be stored into a byte array, so, I…
xmllmx
  • 33,981
  • 13
  • 121
  • 269
1
2 3