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 <stdio.h>
#include <stdlib.h>

int main(void)
{
    // 32 bit 

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

    unsigned long n = 1991994; //0x001E653A;

    printf("%X\n", n);

    n = _byteswap_ulong(n);

    printf("%X\n", n);
}



/*

001E653A
3A651E00

*/

 



answered Jan 3, 2025 by avibootz
0 votes
#include <stdio.h>

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

int main(void)
{
    // 32 bit 

    unsigned long n = 1991994; //0x001E653A;

    printf("%X\n", n);

    n = toLittleEndian(n);

    printf("%X\n", n);
}




/*

001E653A
3A651E00

*/

 



answered Jan 3, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdint.h>

int main(void)
{
    // 32 bit 
  
    // uint32_t __builtin_bswap32 (uint32_t x) // GCC
  
    uint32_t n = 1991994; //0x001E653A;
  
    printf("%X\n", n);
  
    n = __builtin_bswap32(n);
  
    printf("%X\n", n);
}
  
  
  
/*

001E653A
3A651E00
  
*/
 

 



answered Jan 3, 2025 by avibootz
...