/* This demo program shows how to read */
/* and write big-endian data */
#include stdio.h
#include stdint.h
static inline uint32_t read_32bit_be( const uint8_t * const ptr_buf )
{
uint32_t byte0;
uint32_t byte1;
uint32_t byte2;
uint32_t byte3;
byte0 = ptr_buf[0];
byte1 = ptr_buf[1];
byte2 = ptr_buf[2];
byte3 = ptr_buf[3];
byte0 <<= 24;
byte1 <<= 16;
byte2 <<= 8;
return ( byte0 | byte1 | byte2 | byte3 );
}
static inline void write_32bit_be( uint8_t * ptr_buf, uint32_t value )
{
ptr_buf[0] = value >> 24;
ptr_buf[1] = value >> 16;
ptr_buf[2] = value >> 8;
ptr_buf[3] = value;
}
int main()
{
uint8_t buffer[4];
uint32_t val_32bit;
val_32bit = UINT32_MAX;
/*write the 32 bit value to the buffer*/
write_32bit_be( buffer, val_32bit );
val_32bit = 0;
/*Retrieve the big endian value from the buffer*/
val_32bit = read_32bit_be( buffer );
printf( "Big-Endian value - %X", val_32bit );
return 0;
}
Monday, 8 March 2021
Big Endian Read and Write
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment