`

Deal with big-endian and little-endian order

    博客分类:
  • Java
 
阅读更多
Java virtual machine always used big-endian, Intel x86 used little-endian.

public class Swab {
  public final static int swabInt(int v) {
    return  (v >>> 24) | (v << 24) | 
      ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00);
    }

  public static void main(String argv[]) {
    //  before 0x01020304
    //    after  0x04030201
    int v = 0x01020304;
    System.out.println("before : 0x" + Integer.toString(v,16));
    System.out.println("after  : 0x" + Integer.toString(swabInt(v),16));
    }
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics