手记

进制转化以及数据类型的转换

位运算威力:http://blog.csdn.net/iukey/article/details/7195265

大小端详解:http://blog.csdn.net/zhaoshuzhaoshu/article/details/37600857

package com.jiayou;

public class RadixMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
     //十进制转化为其他进制
        /*System.out.println(Integer.toBinaryString(112));
        System.out.println(Integer.toHexString(112));
        System.out.println(Integer.toOctalString(112));
        //其他进制转化为十进制
        System.out.println(Integer.parseInt("110011", 2));
        System.out.println(Integer.parseInt("45", 8));
        System.out.println(Integer.parseInt("A8", 16));*/
        byte[] arr=RadixMain.int2Bytes(8143);
        System.out.println(arr[0]+","+arr[1]+","+arr[2]+","+arr[3]);
        System.out.println(RadixMain.bytes2Int(arr));
    }
    //int转化为byte[]
    public static byte[] int2Bytes(int id){
        byte[] arr=new byte[4];
//      arr[0]=(byte)((int)(id>>0*8)&0xff);
//      arr[1]=(byte)((int)(id>>1*8)&0xff);
//      arr[2]=(byte)((int)(id>>2*8)&0xff);
//      arr[3]=(byte)((int)(id>>3*8)&0xff);
//      return arr;
        for(int i=0;i<arr.length;i++){
            arr[i]=(byte)((int)(id>>i*8)&0xff);
        }
        return arr;
    }
    //byte[]转化为int
    public static int bytes2Int(byte[] arr){
        //int rs0=(int)((arr[0]&0xff)<<0*8);
        //int rs1=(int)((arr[1]&0xff)<<1*8);
        //int rs2=(int)((arr[2]&0xff)<<2*8);
        //int rs3=(int)((arr[3]&0xff)<<3*8);
        //return rs0+rs1+rs2+rs3;
        int result=0;
        for(int i=0;i<arr.length;i++){
            result+=(int)((arr[i]&0xff)<<i*8);
        }
        return result;
    }

}
0人推荐
随时随地看视频
慕课网APP