Q:

How to Convert little-endian to big-endian vice versa in C?

0

How to Convert little-endian to big-endian vice versa in C?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

We can convert little-endian to big-endian or vice versa using the C programs. So let us see few ways to convert one endian to another.

#include <stdio.h>
#include <inttypes.h>
//Function to change one endian to another
uint32_t ChangeEndianness(uint32_t u32Value)
{
    uint32_t u32Result = 0;
    u32Result |= (u32Value & 0x000000FF) << 24;
    u32Result |= (u32Value & 0x0000FF00) << 8;
    u32Result |= (u32Value & 0x00FF0000) >> 8;
    u32Result |= (u32Value & 0xFF000000) >> 24;
    return u32Result;
}
int main()
{
    uint32_t u32CheckData  = 0x11223344;
    uint32_t u32ResultData =0;
    //swap the data
    u32ResultData = ChangeEndianness(u32CheckData);
    //converted data
    printf("0x%x\n",u32ResultData);
    return 0;
}

Output:

0x44332211

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Embedded C interview questions and answers (2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is static memory allocation and dynamic memor... >>
<< Write a C program to check the endianness of the s...