Java 中的各种进制转化 EncodeUtil

 EncodeUtil Java 中常用的进制转化

package cn.shijinet.kunlun.packagecontrol.util;

/**
 * @author: Eric
 * @Date: 2018-12-14 15:00
 **/
public class EncodeUtil {

    public static String convertAsc2ToString(String asc2) {

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < asc2.length() - 1; i += 2) {

            String output = asc2.substring(i, (i + 2));

            int decimal = Integer.parseInt(output, 16);

            sb.append((char) decimal);
        }
        return sb.toString();
    }

    public static String convertHexToString(String hex) {

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < hex.length() - 1; i += 2) {

            String output = hex.substring(i, (i + 2));

            int decimal = Integer.parseInt(output, 16);

            sb.append(decimal);
        }
        return sb.toString();
    }


    public static String convertDECToString(String dec) {

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < dec.length() - 1; i += 2) {

            int output = Integer.parseInt(dec.substring(i, (i + 2)));

            String s = Integer.toHexString(output);

            sb.append(s);
        }
        return sb.toString();
    }

    public static int[] getPositions(String[] str) {
        int[] positions = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            positions[i] = Integer.parseInt(str[i]) * 2;
        }
        return positions;
    }

    private static String hexStr =  "0123456789ABCDEF";
    private static String[] binaryArray =
            {"0000","0001","0010","0011",
                    "0100","0101","0110","0111",
                    "1000","1001","1010","1011",
                    "1100","1101","1110","1111"};

    /**
     * 转换为二进制字符串
     */
    public static String bytes2BinaryStr(byte[] bArray){

        StringBuilder outStr = new StringBuilder();
        int pos = 0;
        for(byte b:bArray){
            //高四位
            pos = (b&0xF0)>>4;
            outStr.append(binaryArray[pos]);
            //低四位
            pos=b&0x0F;
            outStr.append(binaryArray[pos]);
        }
        return outStr.toString();

    }

    /**
     * 将二进制转换为十六进制字符输出
     */
    public static String binary2HexString(byte[] bytes){

        String result = "";
        String hex = "";
        for(int i=0;i<bytes.length;i++){
            //字节高4位
            hex = String.valueOf(hexStr.charAt((bytes[i]&0xF0)>>4));
            //字节低4位
            hex += String.valueOf(hexStr.charAt(bytes[i]&0x0F));
            result +=hex+" ";
        }
        return result;
    }

    /**
     * 将十六进制转换为字节数组
     */
    public static byte[] hexString2Binary(String hexString){
        //hexString的长度对2取整,作为bytes的长度
        int len = hexString.length()/2;
        byte[] bytes = new byte[len];
        //字节高四位
        byte high = 0;
        //字节低四位
        byte low = 0;

        for(int i=0;i<len;i++){
            //右移四位得到高位
            high = (byte)((hexStr.indexOf(hexString.charAt(2*i)))<<4);
            low = (byte)hexStr.indexOf(hexString.charAt(2*i+1));
            //高地位做或运算
            bytes[i] = (byte) (high|low);
        }
        return bytes;
    }
}
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值