VLCB SDK
An opinionated SDK for VLCB protocol
Loading...
Searching...
No Matches
endian.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5// Convert 32-bit integer from host byte order to network byte order
6static inline uint32_t htonl(uint32_t hostlong) {
7 // Check if the system is little-endian
8 if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) {
9 // Swap byte order for little-endian systems
10 return ((hostlong & 0xFF000000) >> 24) |
11 ((hostlong & 0x00FF0000) >> 8) |
12 ((hostlong & 0x0000FF00) << 8) |
13 ((hostlong & 0x000000FF) << 24);
14 } else {
15 // On big-endian systems, no conversion is needed
16 return hostlong;
17 }
18}
19
20// Convert 16-bit integer from host byte order to network byte order
21static inline uint16_t htons(uint16_t hostshort) {
22 // Check if the system is little-endian
23 if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) {
24 // Swap byte order for little-endian systems
25 return ((hostshort & 0xFF00) >> 8) |
26 ((hostshort & 0x00FF) << 8);
27 } else {
28 // On big-endian systems, no conversion is needed
29 return hostshort;
30 }
31}
32
33// Convert 32-bit integer from network byte order to host byte order
34static inline uint32_t ntohl(uint32_t netlong) {
35 // The implementation is the same as htonl (reversed operation)
36 return htonl(netlong);
37}
38
39// Convert 16-bit integer from network byte order to host byte order
40static inline uint16_t ntohs(uint16_t netshort) {
41 // The implementation is the same as htons (reversed operation)
42 return htons(netshort);
43}
static uint32_t ntohl(uint32_t netlong)
Definition endian.h:34
static uint16_t ntohs(uint16_t netshort)
Definition endian.h:40
static uint32_t htonl(uint32_t hostlong)
Definition endian.h:6
static uint16_t htons(uint16_t hostshort)
Definition endian.h:21