隨筆-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轉(zhuǎn)換之后的負(fù)數(shù)部分。
            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)));
           }
           
           /**
            * 打印二進(jìn)制數(shù)組
            * @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數(shù)據(jù),用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)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 井冈山市| 日照市| 尤溪县| 萝北县| 偏关县| 昌图县| 含山县| 嘉义县| 柯坪县| 独山县| 探索| 巫溪县| 莫力| 沽源县| 日喀则市| 平山县| 新营市| 织金县| 榕江县| 洛扎县| 阿坝县| 新蔡县| 澄迈县| 广州市| 贞丰县| 柘城县| 芮城县| 深水埗区| 江北区| 密山市| 汉阴县| 新田县| 黑龙江省| 德江县| 武强县| 杨浦区| 高安市| 团风县| 乌鲁木齐县| SHOW| 宝应县|