convert big endian to little endian in C [without using provided func] [closed] convert big endian to little endian in C [without using provided func] [closed] c c

convert big endian to little endian in C [without using provided func] [closed]


Assuming what you need is a simple byte swap, try something like

Unsigned 16 bit conversion:

swapped = (num>>8) | (num<<8);

Unsigned 32-bit conversion:

swapped = ((num>>24)&0xff) | // move byte 3 to byte 0                    ((num<<8)&0xff0000) | // move byte 1 to byte 2                    ((num>>8)&0xff00) | // move byte 2 to byte 1                    ((num<<24)&0xff000000); // byte 0 to byte 3

This swaps the byte orders from positions 1234 to 4321. If your input was 0xdeadbeef, a 32-bit endian swap might have output of 0xefbeadde.

The code above should be cleaned up with macros or at least constants instead of magic numbers, but hopefully it helps as is

EDIT: as another answer pointed out, there are platform, OS, and instruction set specific alternatives which can be MUCH faster than the above. In the Linux kernel there are macros (cpu_to_be32 for example) which handle endianness pretty nicely. But these alternatives are specific to their environments. In practice endianness is best dealt with using a blend of available approaches


By including:

#include <byteswap.h>

you can get an optimized version of machine-dependent byte-swapping functions.Then, you can easily use the following functions:

__bswap_32 (uint32_t input)

or

__bswap_16 (uint16_t input)


#include <stdint.h>//! Byte swap unsigned shortuint16_t swap_uint16( uint16_t val ) {    return (val << 8) | (val >> 8 );}//! Byte swap shortint16_t swap_int16( int16_t val ) {    return (val << 8) | ((val >> 8) & 0xFF);}//! Byte swap unsigned intuint32_t swap_uint32( uint32_t val ){    val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );     return (val << 16) | (val >> 16);}//! Byte swap intint32_t swap_int32( int32_t val ){    val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );     return (val << 16) | ((val >> 16) & 0xFFFF);}

Update : Added 64bit byte swapping

int64_t swap_int64( int64_t val ){    val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );    val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );    return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);}uint64_t swap_uint64( uint64_t val ){    val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );    val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );    return (val << 32) | (val >> 32);}