Advertisement
DrAungWinHtut

changeEndianess.cpp

Mar 30th, 2023
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. uint32_t changeEndianness(const uint32_t& value);
  4. int main() {
  5.     cout << "Hello, World!" << endl;
  6.     uint32_t little_endian = 0x0A0B0C0D;
  7.     uint32_t big_endian = 0x00000000; //=0;
  8.     big_endian = changeEndianness(little_endian);
  9.  
  10.     printf("Little Endian = %x => Big Endian = %x",little_endian,big_endian);
  11.  
  12.     return 0;
  13. }
  14.  
  15. uint32_t changeEndianness(const uint32_t& value)
  16. {
  17.     uint32_t ans = 0x00000000;
  18.     uint32_t temp = 0x00000000;
  19.     temp = value & 0x000000FF; //0x0000000D
  20.     temp = temp <<24; //0x0D000000
  21.     ans = ans | temp; //0x0D000000
  22.  
  23.  
  24.     temp = 0x00000000;
  25.     temp = value & 0x0000FF00; //0x00000C00
  26.     temp = temp <<8; //0x000C0000
  27.     ans = ans | temp; //0x0D0C0000
  28.  
  29.     temp = 0x00000000;
  30.     temp = value & 0x00FF0000; //0x000B0000
  31.     temp = temp>>8; //0x00000B00
  32.     ans = ans | temp; //0x0D0C0B00
  33.  
  34.     temp = 0x00000000;
  35.     temp = value & 0xFF000000; //0x0A000000 Masking
  36.     temp = temp>>24; //0x0000000A
  37.     ans = ans | temp; //0x0D0C0B0A
  38.  
  39.     return ans;
  40.  
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement