How to swap bytes in MCU

Hardware issues, electronic components, schemas, Arduino, STM32, Robots, Sensors
Post Reply
Administrator
Site Admin
Posts: 81
Joined: 26-Feb-2014, 17:54

How to swap bytes in MCU

Post by Administrator » 10-Jan-2023, 23:05

Code: Select all

/// Network to host byte order conversion.
#if defined (__CC_ARM)
  // ARM Compiler 4/5
  #define ntohl(v)              (uint32_t)(__rev(v))
  #define ntohs(v)              (uint16_t)(__rev(v) >> 16)
#else
  // ARM Compiler 6
  #define ntohl(v)              __builtin_bswap32((uint32_t)(v))
  #define ntohs(v)              __builtin_bswap16((uint16_t)(v))
#endif

Code: Select all

/// Host to network byte order conversion.
#ifndef htons
	#define htons(v)                ntohs(v)
	#define htonl(v)                ntohl(v)
#endif
static inline SWAPB(uint16_t w) { return ntohs(w); }

Post Reply