进制转化工具类

进制转化工具类

import cn.hutool.core.convert.Convert;

/**
 * 进制转化工具
 */
public class ParseSystemUtil {
    /**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase()+" ");
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * 二进制转十进制
     *
     * @param number
     * @return
     */
    public static int binary2Decimal(String number) {
        return scale2Decimal(number, 2);
    }

    /**
     * 其他进制转十进制
     *
     * @param number
     * @return
     */
    public static int scale2Decimal(String number, int scale) {
        checkNumber(number);
        if (2 > scale || scale > 32) {
            throw new IllegalArgumentException("scale is not in range");
        }
        // 不同其他进制转十进制,修改这里即可
        int total = 0;
        String[] ch = number.split("");
        int chLength = ch.length;
        for (int i = 0; i < chLength; i++) {
            total += Integer.valueOf(ch[i]) * Math.pow(scale, chLength - 1 - i);
        }
        return total;

    }

    /**
     * 二进制转十进制
     *
     * @param number
     * @return
     */
    public static String decimal2Binary(int number) {
        return decimal2Scale(number, 2);
    }

    /**
     * 十进制转其他进制
     *
     * @param number
     * @param scale
     * @return
     */
    public static String decimal2Scale(int number, int scale) {
        if (2 > scale || scale > 32) {
            throw new IllegalArgumentException("scale is not in range");
        }
        String result = "";
        while (0 != number) {
            result = number % scale + result;
            number = number / scale;
        }

        return result;
    }

    public static String two2sixteen(String number) {
        int i = Integer.parseInt(number, 2);
        return Integer.toHexString(i);
    }

    public static void checkNumber(String number) {
        String regexp = "^\\d+$";
        if (null == number || !number.matches(regexp)) {
            throw new IllegalArgumentException("input is not a number");
        }
    }

    /**
     * 16进制字符串转化二进制字符串
     *
     * @param hexString
     * @return
     */
    public static String hexStr2String(String hexString) {
        if (hexString == null || hexString.length() % 2 != 0) {
            return null;
        }
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }
    private static String[] binaryArray =
            {"0000","0001","0010","0011",
                    "0100","0101","0110","0111",
                    "1000","1001","1010","1011",
                    "1100","1101","1110","1111"};
    /**
     * @param bArray
     * @return 转换为二进制字符串
     */
    public static String bytes2BinaryStr(byte[] bArray) {

        String outStr = "";
        int pos = 0;
        for (byte b : bArray) {
            //高四位
            pos = (b & 0xF0) >> 4;
            outStr += binaryArray[pos];
            //低四位
            pos = b & 0x0F;
            outStr += binaryArray[pos];
        }
        return outStr;

    }


    /**
     * 二制度字符串转字节数组,如 101000000100100101110000 -> A0 09 70
     * @param input 输入字符串。
     * @return 转换好的字节数组。
     */
    public static byte[] string2bytes(String input) {
        StringBuilder in = new StringBuilder(input);
        // 注:这里in.length() 不可在for循环内调用,因为长度在变化
        int remainder = in.length() % 8;
        if (remainder > 0)
            for (int i = 0; i < 8 - remainder; i++)
                in.append("0");
        byte[] bts = new byte[in.length() / 8];

        // Step 8 Apply compression
        for (int i = 0; i < bts.length; i++)
            bts[i] = (byte) Integer.parseInt(in.substring(i * 8, i * 8 + 8), 2);

        return bts;
    }

    public static void main(String[] args) {
        int i = Integer.parseInt("0010000", 2);
        String s = Integer.toHexString(i);
        System.out.println(s);
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 C++ 程序,用于将二进制和十六进制相互转换。它使用了两个线程,一个用于输入和输出,另一个用于计算转换结果。您可以使用这个程序作为基础,根据自己的需求进行修改和扩展。 ``` #include <iostream> #include <thread> #include <mutex> #include <bitset> std::mutex mtx; void binaryToHex(std::string binary) { std::bitset<8> bits(binary); int hex = bits.to_ulong(); std::lock_guard<std::mutex> lck(mtx); std::cout << std::hex << hex; } void hexToBinary(std::string hex) { int num = std::stoi(hex, nullptr, 16); std::bitset<8> bits(num); std::lock_guard<std::mutex> lck(mtx); std::cout << bits.to_string(); } int main() { std::string input; std::thread t1, t2; while (true) { std::cout << "Enter a binary or hex number (q to quit): "; std::cin >> input; if (input == "q") break; if (input.size() == 8) { t1 = std::thread(binaryToHex, input); t1.join(); } else if (input.size() == 2) { t2 = std::thread(hexToBinary, input); t2.join(); } std::cout << std::endl; } return 0; } ``` 在这个程序中,我们定义了两个函数 `binaryToHex()` 和 `hexToBinary()`,分别用于将二进制转换为十六进制和将十六进制转换为二进制。这两个函数都采用了 `std::thread` 类来运行,以便在不同的线程中运行它们。这样就可以实现并发处理,提高程序的效率。 注意,在 `main()` 函数中,我们使用了 `std::lock_guard<std::mutex>` 来锁定共享资源(即输出流)。这样可以确保两个线程不会同时访问输出流,从而避免竞态条件和数据损坏。 最后,我们使用一个循环来读取用户输入,并根据输入的长度来决定要使用哪个线程来进行转换。当用户输入 "q" 时,程序退出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值