16 | | I don't know if this is the correct way is to include <machine/endian.h>, maybe someone else has more experience how to use includes which come from the "machine" folder. |
| 16 | This is not the correct way it is just to show that the file is in the default search path. |
| 17 | |
| 18 | The following one also works because it indirectly includes machine/endian.h via <sys/type.h> |
| 19 | |
| 20 | {{{ |
| 21 | #include <stdio.h> |
| 22 | #include <stdint.h> |
| 23 | /* #include <machine/endian.h> */ |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | int main(void) |
| 27 | { |
| 28 | uint16_t a = 0x1234; |
| 29 | printf("%hx\n", htons(a)); |
| 30 | } |
| 31 | |
| 32 | }}} |
| 33 | |
| 34 | The documentation of 'htons' says: |
| 35 | |
| 36 | {{{ |
| 37 | |
| 38 | BYTEORDER(3) BSD Library Functions Manual BYTEORDER(3) |
| 39 | |
| 40 | NAME |
| 41 | htonl, htons, htonll, ntohl, ntohs, ntohll -- convert values between host and network byte order |
| 42 | |
| 43 | LIBRARY |
| 44 | Standard C Library (libc, -lc) |
| 45 | |
| 46 | SYNOPSIS |
| 47 | #include <arpa/inet.h> |
| 48 | |
| 49 | }}} |
| 50 | |
| 51 | This works and would be the conformant way on macOS: |
| 52 | {{{ |
| 53 | #include <stdio.h> |
| 54 | #include <stdint.h> |
| 55 | #include <arpa/inet.h> |
| 56 | |
| 57 | int main(void) |
| 58 | { |
| 59 | uint16_t a = 0x1234; |
| 60 | printf("%hx\n", htons(a)); |
| 61 | } |
| 62 | |
| 63 | }}} |
| 64 | |