Convert Little Endian to Big Endian

前端 未结 13 1860
慢半拍i
慢半拍i 2020-11-27 16:34

I just want to ask if my method is correct to convert from little endian to big endian, just to make sure if I understand the difference.

I have a number which is st

13条回答
  •  一个人的身影
    2020-11-27 16:48

    A Simple C program to convert from little to big

    #include 
    
    int main() {
    unsigned int little=0x1234ABCD,big=0;
    unsigned char tmp=0,l;
    
    printf(" Little endian little=%x\n",little);
    
    for(l=0;l < 4;l++) 
    {
        tmp=0;
        tmp = little | tmp;
        big = tmp | (big << 8);
        little = little >> 8;
    }
    printf(" Big endian big=%x\n",big);
    
    return 0;
    }
    

提交回复
热议问题