byte与进制及基本类型间的转换

来源:互联网 发布:Matlab数据精度 编辑:程序博客网 时间:2024/06/06 02:05

java的API源码中隐含了一些对字节处理的方法,以下复制黏贴或者修改后黏贴的代码。

//把byte[]转成int的方法,off指byte的开始位置    static int getInt(byte[] b, int off) {    return ((b[off + 3] & 0xFF) << 0) +           ((b[off + 2] & 0xFF) << 8) +           ((b[off + 1] & 0xFF) << 16) +           ((b[off + 0]) << 24);        }    //把int转成byte[]的方法    static void putInt(byte[] b, int off, int val) {    b[off + 3] = (byte) (val >>> 0);    b[off + 2] = (byte) (val >>> 8);    b[off + 1] = (byte) (val >>> 16);    b[off + 0] = (byte) (val >>> 24);        }
上面这个是举例,方法来源于Bits.class文件。
以下是这个文件的所有方法。
    /*     * Methods for unpacking primitive values from byte arrays starting at     * given offsets.     */    static boolean getBoolean(byte[] b, int off) {return b[off] != 0;    }        static char getChar(byte[] b, int off) {return (char) (((b[off + 1] & 0xFF) << 0) +        ((b[off + 0]) << 8));    }        static short getShort(byte[] b, int off) {return (short) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0]) << 8));    }        static int getInt(byte[] b, int off) {return ((b[off + 3] & 0xFF) << 0) +       ((b[off + 2] & 0xFF) << 8) +       ((b[off + 1] & 0xFF) << 16) +       ((b[off + 0]) << 24);    }        static float getFloat(byte[] b, int off) {int i = ((b[off + 3] & 0xFF) << 0) +((b[off + 2] & 0xFF) << 8) +((b[off + 1] & 0xFF) << 16) +((b[off + 0]) << 24);return Float.intBitsToFloat(i);    }        static long getLong(byte[] b, int off) {return ((b[off + 7] & 0xFFL) << 0) +       ((b[off + 6] & 0xFFL) << 8) +       ((b[off + 5] & 0xFFL) << 16) +       ((b[off + 4] & 0xFFL) << 24) +       ((b[off + 3] & 0xFFL) << 32) +       ((b[off + 2] & 0xFFL) << 40) +       ((b[off + 1] & 0xFFL) << 48) +       (((long) b[off + 0]) << 56);    }    static double getDouble(byte[] b, int off) {long j = ((b[off + 7] & 0xFFL) << 0) + ((b[off + 6] & 0xFFL) << 8) + ((b[off + 5] & 0xFFL) << 16) + ((b[off + 4] & 0xFFL) << 24) + ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40) + ((b[off + 1] & 0xFFL) << 48) + (((long) b[off + 0]) << 56);return Double.longBitsToDouble(j);    }        /*     * Methods for packing primitive values into byte arrays starting at given     * offsets.     */    static void putBoolean(byte[] b, int off, boolean val) {b[off] = (byte) (val ? 1 : 0);    }    static void putChar(byte[] b, int off, char val) {b[off + 1] = (byte) (val >>> 0);b[off + 0] = (byte) (val >>> 8);    }    static void putShort(byte[] b, int off, short val) {b[off + 1] = (byte) (val >>> 0);b[off + 0] = (byte) (val >>> 8);    }    static void putInt(byte[] b, int off, int val) {b[off + 3] = (byte) (val >>> 0);b[off + 2] = (byte) (val >>> 8);b[off + 1] = (byte) (val >>> 16);b[off + 0] = (byte) (val >>> 24);    }    static void putFloat(byte[] b, int off, float val) {int i = Float.floatToIntBits(val);b[off + 3] = (byte) (i >>> 0);b[off + 2] = (byte) (i >>> 8);b[off + 1] = (byte) (i >>> 16);b[off + 0] = (byte) (i >>> 24);    }    static void putLong(byte[] b, int off, long val) {b[off + 7] = (byte) (val >>> 0);b[off + 6] = (byte) (val >>> 8);b[off + 5] = (byte) (val >>> 16);b[off + 4] = (byte) (val >>> 24);b[off + 3] = (byte) (val >>> 32);b[off + 2] = (byte) (val >>> 40);b[off + 1] = (byte) (val >>> 48);b[off + 0] = (byte) (val >>> 56);    }    static void putDouble(byte[] b, int off, double val) {long j = Double.doubleToLongBits(val);b[off + 7] = (byte) (j >>> 0);b[off + 6] = (byte) (j >>> 8);b[off + 5] = (byte) (j >>> 16);b[off + 4] = (byte) (j >>> 24);b[off + 3] = (byte) (j >>> 32);b[off + 2] = (byte) (j >>> 40);b[off + 1] = (byte) (j >>> 48);b[off + 0] = (byte) (j >>> 56);    }

在DataInputStream里也有部分这种方法。
    //修改自DataInputStream里的方法    public static int toInt(byte[] b,int off) throws EOFException    {        int ch1 = b[off+0];        int ch2 = b[off+1];        int ch3 = b[off+2];        int ch4 = b[off+3];        if ((ch1 | ch2 | ch3 | ch4) < 0)            throw new EOFException();        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));    }

不过这个类源码中只有转成基本类型的方法,仔细看和Bits.class文件里的方法差不多。
系统对基本类型转成的byte是低位保存高位。
比如short类型有16位,第一个byte保存高8位,第二个字节保存低8位。

下面方法来源于网络,没全部验证其正确性。注意:其中有些转byte的方法是把低位保存在低位。



//long类型转成byte数组 

 public static byte[] longToByte(long number) { 
       long temp = number; 
       byte[] b = new byte[8]; 
       for (int i = 0; i < b.length; i++) { 
           b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 
           temp = temp >> 8; // 向右移8位 
       } 
       return b; 
   } 
   
   //byte数组转成long 
   public static long byteToLong(byte[] b) { 
       long s = 0; 
       long s0 = b[0] & 0xff;// 最低位 
       long s1 = b[1] & 0xff; 
       long s2 = b[2] & 0xff; 
       long s3 = b[3] & 0xff; 
       long s4 = b[4] & 0xff;// 最低位 
       long s5 = b[5] & 0xff; 
       long s6 = b[6] & 0xff; 
       long s7 = b[7] & 0xff; 
 
       // s0不变 
       s1 <<= 8; 
       s2 <<= 16; 
       s3 <<= 24; 
       s4 <<= 8 * 4; 
       s5 <<= 8 * 5; 
       s6 <<= 8 * 6; 
       s7 <<= 8 * 7; 
       s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7; 
       return s; 
   } 
 
/** 
    * 注释:int到字节数组的转换! 
    * 
    * @param number 
    * @return 
    */ 
   public static byte[] intToByte(int number) { 
       int temp = number; 
       byte[] b = new byte[4]; 
       for (int i = 0; i < b.length; i++) { 
           b[i] = new Integer(temp & 0xff).byteValue();//将最低位保存在最低位 
           temp = temp >> 8; // 向右移8位 
       } 
       return b; 
   } 
 
   /** 
    * 注释:字节数组到int的转换! 
    * 
    * @param b 
    * @return 
    */ 
   public static int byteToInt(byte[] b) { 
       int s = 0; 
       int s0 = b[0] & 0xff;// 最低位 
       int s1 = b[1] & 0xff; 
       int s2 = b[2] & 0xff; 
       int s3 = b[3] & 0xff; 
       s3 <<= 24; 
       s2 <<= 16; 
       s1 <<= 8; 
       s = s0 | s1 | s2 | s3; 
       return s; 
   } 
 
   /** 
    * 注释:short到字节数组的转换! 
    * 
    * @param s 
    * @return 
    */ 
   public static byte[] shortToByte(short number) { 
       int temp = number; 
       byte[] b = new byte[2]; 
       for (int i = 0; i < b.length; i++) { 
           b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位 
           temp = temp >> 8; // 向右移8位 
       } 
       return b; 
   } 
 
   /** 
    * 注释:字节数组到short的转换! 
    * 
    * @param b 
    * @return 
    */ 
   public static short byteToShort(byte[] b) { 
       short s = 0; 
       short s0 = (short) (b[0] & 0xff);// 最低位 
       short s1 = (short) (b[1] & 0xff); 
       s1 <<= 8; 
       s = (short) (s0 | s1); 
       return s; 

   }


//把byte转成16进制public static final String toHex(byte b) {//32位右移4位再把高28位清零,即求出来的是这个32位中的第四到第八位//  ....0000001111&b return ("" + "0123456789ABCDEF".charAt(0xf & b >> 4) + "0123456789ABCDEF".charAt(b & 0xf));}//把16进制的字符串转成byte[]public static byte[] hexStringToBytes(String hexString) {     if (hexString == null || hexString.equals("")) {         return null;     }     hexString = hexString.toUpperCase();     int length = hexString.length() / 2;     char[] hexChars = hexString.toCharArray();     byte[] d = new byte[length];     for (int i = 0; i < length; i++) {         int pos = i * 2;         d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));     }     return d;  } /**  * Convert char to byte  * @param c char  * @return byte  */   private static byte charToByte(char c) {      return (byte) "0123456789ABCDEF".indexOf(c);  //返回字符的十进制数并转成2进制,1byte等于8位ASCII} //把byte[]转成16进制显示. public static String bytes2HexString(byte[] b) {   String ret = "";   for (int i = 0; i < b.length; i++) {    String hex = Integer.toHexString(b[ i ] & 0xFF);    if (hex.length() == 1) {     hex = '0' + hex;    }    ret += hex.toUpperCase();   }   return ret;  } 


原创粉丝点击