隨筆-77  評論-5  文章-2  trackbacks-0

           

          public class Util {
           final static int LINE_COUNT = 16;
           final static int WORD_COUNT = 2;
           public static StringBuffer toHex(byte b)
           {
            byte factor = 16;
            int v = b & 0xff;//去掉byte轉換之后的負數部分。
            byte high = (byte)( v / factor);
            byte low = (byte)(v % factor);
            StringBuffer buf = new StringBuffer();
            buf.append(toHexLow(high)).append(toHexLow(low));
            return buf;
           }
           private static char toHexLow(byte b)
           {
            if(b > 16 || b < 0 )
            {
             throw new IllegalArgumentException("inpt parameter should less than 16 and greater than 0");
            }
            if(b < 10){
             return (char)('0' + (char)b);
            }
            else{
              return (char)('A' + (b-10));

            }
           }
           
           public static StringBuffer toHex(int val)
           {
            StringBuffer buf = toHex((byte)(val >>24 & 0xff)).append(toHex((byte)(val>>16&0xff)));
            return buf.append(toHex((byte)(val>>8&0xff))).append(toHex((byte)(val & 0xff)));
           }
           
           /**
            * 打印二進制數組
            * @param arr
            * @param off
            * @param len
            */
           public static void printBytes(byte [] arr,int off,int len)
           {
            if(arr == null || len <= 0 || off <0 || off + len > arr.length){
             return;
            }
            
            int count = 0;
            
            for(int i = off; count < len; ++i)
            {
             System.out.print(toHex(arr[i]));
             
             ++ count;
             if(count% WORD_COUNT == 0)
             {
              System.out.print(' ');
             }
             if(count % LINE_COUNT == 0)
             {
              System.out.println();
             }
            }
           }
           
           public static void main(String[] args) {
            byte[] arr = new byte[256];
            for(int i = 0; i < 256;++i )
            {
             
             arr[i] = (byte)i;
             
            }
            
            printBytes(arr,0,256);
            printBytes(arr,240,16);
            
            System.out.println(toHex(1));
            System.out.println(toHex(0xffffffff));
            System.out.println(toHex(0xeeffaacc));
           }
          }




          另外c++寫好的小端序的int數據,用java讀入如此處理
           private static int convertInt(byte[]  arr)
           {
            if(arr == null || arr.length != 4)
            {
             throw new IllegalArgumentException("bytes array error");
            }
            int val = (arr[0] & 0xff) | (arr[1] & 0xff)<<8 | (arr[2] & 0xff)<<16 | (arr[3]&0xff)<<24;
            return val;
           }


          posted on 2011-11-02 21:36 huohuo 閱讀(5397) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 北碚区| 剑阁县| 屯留县| 濮阳市| 永川市| 沅江市| 和龙市| 特克斯县| 冷水江市| 灵丘县| 宁波市| 颍上县| 吴川市| 泸西县| 威远县| 西安市| 来宾市| 临桂县| 永安市| 鸡泽县| 开江县| 平邑县| 乌恰县| 都江堰市| 靖安县| 台安县| 绿春县| 徐州市| 平原县| 丹寨县| 汾西县| 常山县| 潼南县| 沙田区| 隆化县| 高清| 平陆县| 土默特左旗| 澎湖县| 塘沽区| 东城区|