笔记75 | Java字节、十进制、十六进制、字符串之间的相互转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiangyong_1521/article/details/80437167
/*
     * 字节转10进制
     */
    public static int byte2Int(byte b){
        int r = (int)b;
        return r;
    }

    /*
     * 10进制转字节
     */
    public static byte int2Byte(int i){
        byte r = (byte)i;
        return r;
    }

    /*
     * 字节数组转16进制字符串
     */
    public static String bytes2HexString(byte[] b){
        String r = " ";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = "0"+hex;
            }
            r += hex.toUpperCase();
        }
        return r;
    }

    /*
     * 10进制字符串转字节数组
     */
    //字符转为字节
    private static byte charToByte(char c){
        return (byte)"0123456789ABCDEF".indexOf(c);
    }

    //16进制字符串转字节数组
    public static byte[] hexString2Bytes(String hex){

        if ((hex==null)||(hex.equals(""))) {
            return null;
        }else if (hex.length()%2 != 0) {
            return null;
        }else{
            hex = hex.toUpperCase();
            int len = hex.length()/2;
            byte[] b = new byte[len];
            char[] hc = hex.toCharArray();
            for (int i = 0; i < len; i++) {
                int p=2*i; 
                b[i] = (byte)(charToByte(hc[p])<<4 | charToByte(hc[p+1]));
            }
            return b;
        }
    }

    /*
     * 字节数组转字符串
     */
    public static String bytes2String(byte[] b) throws UnsupportedEncodingException{
        String r = null;
        r = new String(b,"GBK");
        return r;
    }

    /*
     * 字符串转字节组
     */
    public static byte[] string2Bytes(String s){
        byte[] r = s.getBytes();
        return r;
    }

    /*
     * 16进组字符串转字符串
     */
    public static String hex2String(String hex){
        String r = bytes2HexString(hexString2Bytes(hex));
        return r;
    }

    /*
     * 字符串转16进组字符串
     */
    public static String string2HexString(String s){
        String r = bytes2HexString(string2Bytes(s));
        return r;
    }

    private  void  LoandFragment(Fragment fragment){
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fm, fragment);
        ft.commitAllowingStateLoss();
    }

    private void initDate(){ 
        byte b1 = (byte) 45;
        Log.i("md", "1.字节转10进制:" + byte2Int(b1));
        int i = 89;
        Log.i("md", "2.10进制转字节:" + int2Byte(i));

        byte[] b2 = new byte[]{(byte)0xFF, (byte)0x5F, (byte)0x6, (byte)0x5A};
        Log.i("md", "3.字节数组转16进制字符串:" + bytes2HexString(b2));

        String s1 = new String("1DA47C");
        Log.i("md", "4.16进制字符串转字节数组:" + Arrays.toString(hexString2Bytes(s1)));

        try {
            Log.i("md", "5.字节数组转字符串:" + bytes2String(b2));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.i("md", "6.字符串转字节数组:" + Arrays.toString(string2Bytes(s1)));

        Log.i("md", "7.16进制字符串转字符串:" + hex2String(s1));

        String s2 = new String("Hello!");
        Log.i("md", "8.字符串转16进制字符串:" + string2HexString(s2));
    }

猜你喜欢

转载自blog.csdn.net/xiangyong_1521/article/details/80437167