Java 进制转换

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


/**
 * 十进制转十六进制
 **/
public static String decToHex(Integer dec) {
    return Integer.toHexString(dec);
}

/**
 * 十六进制转十进制
 **/
public static Long hexToDec(String hex) {
    return Long.parseLong(hex, 16);
}

/**
 * 十进制转二进制
 **/
public static String decToBinary(Integer dec) {
    return Integer.toBinaryString(dec);
}

/**

 * 十六进制转二进制
 **/
public static String hexToBinary(String hex) {
    return decToBinary(hexToDec(hex).intValue());
}

/**
 * 将字符串转换为十六进制存储为byte数组
 **/
public static byte[] getBytes(String str) {
    if (str == null || "".equals(str.trim())) {
        return new byte[0];
    }
    byte[] bytes = new byte[str.length() / 2];
    for (int i = 0; i < str.length() / 2; i++) {
        String subStr = str.substring(i * 2, i * 2 + 2);
        bytes[i] = (byte) Integer.parseInt(subStr, 16);
    }
    return bytes;
}

/**
 * 将存储为byte类型的数组转换为字符串
 **/
public static String bytesToHexFun2(byte[] bytes) {
    char[] buf = new char[bytes.length * 2];
    int index = 0;
    for (byte b : bytes) {
        buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
        buf[index++] = HEX_CHAR[b & 0xf];
    }
    return new String(buf);
}


/**
 * 将十六进制字符串转换为二进制
 **/
public static String hexStrToBinary(String hexNum) {
    char[] chs = {'0', '1'};
    String str = "0123456789ABCDEF";
    char[] charArray = hexNum.toUpperCase().toCharArray();
    int pos = charArray.length * 4;
    char[] binaryArray = new char[pos];
    for (int i = charArray.length - 1; i >= 0; i--) {
        int temp = str.indexOf(charArray[i]);
        for (int j = 0; j < 4; j++) {
            binaryArray[--pos] = chs[temp & 1];
            temp = temp >>> 1;
        }
    }
    return new String(binaryArray);
}

/**
 * 二进制字符串转十六进制字符串
 **/
public static String turn2to16(String str) {
    StringBuilder sum = new StringBuilder();

    int t = str.length() % 4;
    if (t != 0) {
        for (int i = str.length(); i - 4 >= 0; i = i - 4) {
            String s = str.substring(i - 4, i);
            int tem = Integer.parseInt(String.valueOf(s), 2);
            sum.insert(0, Integer.toHexString(tem).toUpperCase());
        }
        String st = str.substring(0, t);

        int tem = Integer.parseInt(String.valueOf(st), 2);
        sum.insert(0, Integer.toHexString(tem).toUpperCase());

    } else {
        for (int i = str.length(); i - 4 >= -1; i = i - 4) {
            String s = str.substring(i - 4, i);
            int tem = Integer.parseInt(String.valueOf(s), 2);
            sum.insert(0, Integer.toHexString(tem).toUpperCase());
        }
    }
    return sum.toString();
}

public static String hexStringToString(String s) {
    if (s == null || s.equals("")) {
        return null;
    }
    s = s.replace(" ", "");
    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");
        new String();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return s;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值