各种进制转换

/**
     * string字符串转成ASCII码格式
     *
     * @autnor zyb
     * created at 2017/2/21 17:35
     */
    public static String strToAsc(String raw) {
        if (raw == null) {
            return "";
        }
        String compactHex = "", hexChar = "";
        for (int i = 0; i < raw.length(); i++) {
            hexChar = Integer.toHexString(raw.charAt(i));
            compactHex += (hexChar.length() == 1 ? '0' + hexChar : hexChar);
        }
        return compactHex.trim();
    }

    /**
     * 十六进制字符串转换字符串    7B22616464496E666F223A7B22726    ====>>>>   {"addInfo":{"result_code":"-1","err_code":"0"}
     *  35353637 ==> 5567
     * @return String
     */
    public static String toStringHex(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
                        i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "utf-8");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();哈哈,
        }
        return s;
    }

    /**
     * 数组转换成十六进制字符串
     *
     * @return HexString
     */
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 十六进制字符串(ASCII码数据)转字节数组
     * 343536373839 ==>  byte[] = {,,,,,};
     * @autnor zyb
     * created at 2017/3/27 15:00
     */
    public static byte[] str2byte(String asc) {
        int len = asc.length();
        int mod = len % 2;
        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }
        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }
        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;
        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else if ((abt[2 * p] >= 'A') && (abt[2 * p] <= 'Z')) {
                j = abt[2 * p] - 'A' + 0x0a;
            } else {
                j = abt[2 * p] - '0';
            }

            if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else if ((abt[2 * p + 1] >= 'A') && (abt[2 * p + 1] <= 'Z')) {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            } else {
                k = abt[2 * p + 1] - '0';
            }

            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }

    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * 转换字节数组为16进制字串
     *
     * @param b 字节数组
     * @return 16进制字串
     */
    public static String byteArrayToHexStr(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexStr(b[i]));
        }
        return resultSb.toString();
    }

    private static String byteToHexStr(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }
    /**
     * 生成随机数
     * @autnor zyb
     * created at 2017/3/27 15:02
     */
    public static void randomBytes(byte[] in) {
        Random random = new Random();
        random.nextBytes(in);
    }
    /**
     * int转byte数组
     * @autnor zyb
     * created at 2017/3/27 15:04
     */
    public static byte[] intToByteShort(int nValue) {
        byte byteVarray[];
        byteVarray = new byte[2];
        byteVarray[1] = (byte) (nValue & 0xff);
        byteVarray[0] = (byte) ((nValue >> 8) & 0xff);
        return byteVarray;
    }
    /**
     * 异或
     * @autnor zyb
     * created at 2017/3/27 15:05
     */
    public static byte[] xor(byte[] a, byte[] b, int len) {
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++) {
            result[i] = (byte) (a[i] ^ b[i]);
        }
        return result;
    }
    /**
     * 字节数组转字符串
     * @autnor zyb
     * created at 2017/3/27 15:05
     */
    public static String bcd2Str(byte[] bytes) {
        if (bytes == null) return null;         //add by liuzujun
        StringBuffer temp = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            byte left = (byte) ((bytes[i] & 0xf0) >>> 4);
            byte right = (byte) (bytes[i] & 0x0f);
            if (left >= 0x0a && left <= 0x0f) {
                left -= 0x0a;
                left += 'A';
            } else {
                left += '0';
            }

            if (right >= 0x0a && right <= 0x0f) {
                right -= 0x0a;
                right += 'A';
            } else {
                right += '0';
            }

            temp.append(String.format("%c", left));
            temp.append(String.format("%c", right));
        }
        return temp.toString();
    }
    /**
     * 字节数组复制
     * @autnor zyb
     * created at 2017/3/27 15:07
     */
    public static boolean cmpByteArray(byte[] a, int aOffset, byte[] b,
                                       int bOffset, int len) {
        if ((aOffset + len) > a.length || (bOffset + len) > b.length) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            if (a[aOffset + i] != b[bOffset + i]) {
                return false;
            }
        }

        return true;
    }
    /**
     * 整形转字节数组
     * @autnor zyb
     * created at 2017/3/27 15:07
     */
    public static void int2ByteArray(int x, byte[] to, int offset) {
        to[offset] = (byte) ((x >>> 24) & 0xff);
        to[offset + 1] = (byte) ((x >>> 16) & 0xff);
        to[offset + 2] = (byte) ((x >>> 8) & 0xff);
        to[offset + 3] = (byte) (x & 0xff);
    }
    /**
     * short类型转字节数组
     * @autnor zyb
     * created at 2017/3/27 15:07
     */
    public static void short2ByteArray(short x, byte[] to, int offset) {
        to[offset] = (byte) ((x >>> 8) & 0xff);
        to[offset + 1] = (byte) (x & 0xff);
    }
    /**
     *
     * @autnor zyb
     * created at 2017/3/27 15:07
     */
    public static int intFromByteArray(byte[] from, int offset) {
        return ((from[offset] << 24) & 0xff000000)
                | ((from[offset + 1] << 16) & 0xff0000)
                | ((from[offset + 2] << 8) & 0xff00)
                | (from[offset + 3] & 0xff);
    }

    public static short shortFromByteArray(byte[] from, int offset) {
        return (short) (((from[offset] << 8) & 0xff00) | (from[offset + 1] & 0xff));
    }
    /**
     * 判断大小值
     * @autnor zyb
     * created at 2017/3/27 15:08
     */
    public static int min(int a, int b) {
        return (a < b) ? a : b;
    }

    public static void prnHexStr(String tips, byte[] bArray) {
        if (bArray == null) return;
        StringBuilder sb = new StringBuilder();
        Log.i(TAG, tips);
        for (int iCnt = 0; iCnt < bArray.length; iCnt++) {
            sb.append(byteToHexStr(bArray[iCnt]).toUpperCase());
            sb.append(" ");
        }
        Log.i(TAG, sb.toString());
    }

    /**
     * 格式化字符串,左补齐
     *
     * @param paddingCha 待前补的字符
     * @param length     补齐后的总长度
     * @param content    原始字符串
     * @return 补齐后的字符串
     */
    public static String paddingLeft(char paddingCha, long length,
                                     String content) {
        if (content.length() > length) {
            return content;
        }
        String str = "";
        for (int i = 0; i < length - content.length(); i++) {
            str += paddingCha;
        }
        str += content;
        return str;
    }

    /**
     * 格式化字符串,右补齐
     *
     * @param paddingCha 待前补的字符
     * @param length     补齐后的总长度
     * @param content    原始字符串
     * @return 补齐后的字符串
     */
    public static String paddingRight(char paddingCha, long length,
                                      String content) {
        if (content.length() > length) {
            return content;
        }

        String str = content;
        for (int i = 0; i < length - content.length(); i++) {
            str += paddingCha;
        }
        return str;
    }

 

 

注:如有问题可以回复,看到第一时间分析解决,码农不易,感觉对您有用,帮助到您,可否打赏一杯可乐,在此谢过诸位,愿诸君终成大神,前程似锦~~~

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值