您的位置 首页 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);


    }
}  

文章来源:智云一二三科技

文章标题:Java数字类进制转换、类型转换

文章地址:https://www.zhihuclub.com/187122.shtml

关于作者: 智云科技

热门文章

网站地图