Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,817 answers

573 users

How to convert a 32-bit number between big-endian and little-endian values in C++

3 Answers

0 votes
#include <iostream>

int main(void)
{
    // 32 bit 

    // unsigned long _byteswap_ulong(unsigned long value); // VS

    unsigned long n = 1991994; //0x001E653A;

    std::cout << std::hex << std::uppercase << n << "\n";

    n = _byteswap_ulong(n);

    std::cout << std::hex << n << "\n";
}



/*

001E653A
3A651E00

*/

 



answered Jan 2, 2025 by avibootz
edited Jan 2, 2025 by avibootz
0 votes
#include <iostream>

uint32_t toLittleEndian(uint32_t value) {
    return ((value >> 24) & 0x000000FF) |
           ((value >> 8) & 0x0000FF00) |
           ((value << 8) & 0x00FF0000) |
           ((value << 24) & 0xFF000000);
}

int main(void)
{
    // 32 bit 

    unsigned long n = 1991994; //0x001E653A;

    std::cout << std::hex << std::uppercase << n << "\n";

    n = toLittleEndian(n);

    std::cout << std::hex << n << "\n";
}


/*

001E653A
3A651E00

*/

 



answered Jan 2, 2025 by avibootz
0 votes
#include <iostream>
#include <cstdint>
 
int main(void)
{
    // 32 bit 
 
    // uint32_t __builtin_bswap32 (uint32_t x) // GCC
 
    uint32_t n = 1991994; //0x001E653A;
 
    std::cout << std::hex << std::uppercase << n << "\n";
 
    n = __builtin_bswap32(n);
 
    std::cout << std::hex << n << "\n";
}
 
 
 
/*
 
001E653A
3A651E00
 
*/

 



answered Jan 2, 2025 by avibootz
...