Converting Little Endian to Big Endian

后端 未结 6 1407
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 19:32

All,

I have been practicing coding problems online. Currently I am working on a problem statement Problems where we need to convert Big Endian <-> little endian.

6条回答
  •  青春惊慌失措
    2020-11-27 20:00

    I think this can also help:

    int littleToBig(int i)
    {
        int b0,b1,b2,b3;
    
        b0 = (i&0x000000ff)>>0;
        b1 = (i&0x0000ff00)>>8;
        b2 = (i&0x00ff0000)>>16;
        b3 = (i&0xff000000)>>24;
    
        return ((b0<<24)|(b1<<16)|(b2<<8)|(b3<<0));
    }
    

提交回复
热议问题