Java数字类进制转换、类型转换

Java数字类进制转换、类型转换

之前做tcp通讯功能要发送byte数组遇到一些进制转换,类型的问题,现在整理分享一下。

package com.utils;

import java.math.BigInteger;

public class Utils {

    /**
     * 获取字节数组字节累加总和
     *
     * @param bytes
     * @return
     */
    public static byte getCheckCode(byte[] bytes) {
        int sum = 0;
        for (int i = 0; i < bytes.length; i++) {
            sum = sum + bytes[i];
        }
        System.out.println("sum:" + sum);
        String hex = Integer.toHexString(sum & 0xff);
        System.out.println("hex:" + hex);
        byte[] a = hexStringToBytes(hex);
        printHexString(a);
        return a[0];
    }

    /**
     * 10进制转16进制
     *
     * @param numb
     * @return
     */
    public static String encodeHEX(Integer numb) {
        String hex = Integer.toHexString(numb);
        return hex;
    }

    /**
     * 16进制转10进制
     *
     * @param hexs
     * @return
     */
    public static int decodeHEX(String hexs) {
        BigInteger bigint = new BigInteger(hexs, 16);
        int numb = bigint.intValue();
        return numb;
    }


    /**
     * 两位16进制字符串转byte数组
     *
     * @param hex
     * @return
     */
    public static byte[] hexStringToBytes(String hex) {
        if ("".equals(hex) && hex.length() == 0) {
            return null;
        }
        hex = hex.replace(" ", "").toUpperCase();
        int length;
        if (hex.length() % 2 != 0) {
            hex = '0' + hex;
        }
        length = hex.length() / 2;
        char[] hexChars = hex.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * char转byte
     *
     * @param c
     * @return
     */
    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 打印16进制字节数组
     *
     * @param b
     */
    public static void printHexString(byte[] b) {
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xff);
            if (hex.length() == 1)
                hex = '0' + hex;
            System.out.println(hex.toUpperCase());
        }

    }

    /**
     * 十六进制字符串转换成int类型
     *
     * @param hex
     * @return
     * @throws Exception
     */
    public static int hex2Int(String hex) throws Exception {
        int sum = 0;
        int allSum = 0;
        for (int i = 0; i < hex.length(); i++) {
            sum = converCharToInt(hex.charAt(i));
            for (int j = 0; j < hex.length() - i - 1; j++) {
                sum = sum * 16;
            }
            allSum = allSum + sum;
        }
        return allSum;
    }

    public static int converCharToInt(char hexc) {
        int value = 0;
        switch (hexc) {
            case '0':
                value = 0;
                break;
            case '1':
                value = 1;
                break;
            case '2':
                value = 2;
                break;
            case '3':
                value = 3;
                break;
            case '4':
                value = 4;
                break;
            case '5':
                value = 5;
                break;
            case '6':
                value = 6;
                break;
            case '7':
                value = 7;
                break;
            case '8':
                value = 8;
                break;
            case '9':
                value = 9;
                break;
            case 'A':
                value = 10;
                break;
            case 'B':
                value = 11;
                break;
            case 'C':
                value = 12;
                break;
            case 'D':
                value = 13;
                break;
            case 'E':
                value = 14;
                break;
            case 'F':
                value = 15;
                break;
            case 'a':
                value = 10;
                break;
            case 'b':
                value = 11;
                break;
            case 'c':
                value = 12;
                break;
            case 'd':
                value = 13;
                break;
            case 'e':
                value = 14;
                break;
            case 'f':
                value = 15;
                break;
        }
        return value;

    }

    /**
     * byte数组转int
     *
     * @param bytes
     * @return
     */
    public static int byteArrayToInt(byte[] bytes) {
        int value = 0;
        //由高位到低位
        for (int i = 0; i < bytes.length; i++) {
            int shift = (bytes.length - 1 - i) * 8;
            value = (bytes[i] & 0x000000FF) << shift;
        }
        return value;
    }

    /**
     * int到byte[]
     *
     * @param i
     * @return
     */
    public static byte[] intToByteArray(int i) {
        byte[] result = new byte[4];
        //由高位到低位
//        result[0] = (byte)((i >> 24) & 0xFF);
//        result[1] = (byte)((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }

    public static void printBytes(byte[] b) {
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xff);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            System.out.println("b[i]:" + hex);
        }
    }


    public static void main(String[] args) {
//        byte[] a=intToByteArray(300);
//        printBytes(a);
//        int b=byteArrayToInt(a);
//        System.out.println(b);


    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

donggela

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值