Android IC卡读写卡 RFID NFC BCC异或和校验值(异或校验法) 取反码 16进制 2进制转换工具

package com.maple.demo.serialport;

import android.util.Log;

/**
 * Created by maple on 2020-09-08 11:03
 * E-Mail Address:[email protected]
 */
public class BinaryHexUtils {


    /**
     * 获取两位16进制字符串的反码
     *
     * @param hexString 两位16进制字符串 8bit
     * @return 两位16进制字符串的反码
     */
    public static String getHexReverse(String hexString) {
        String ss = "0123456789ABCDEF";
        char[] ch = hexString.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (!ss.contains(String.valueOf(ch[i]).toUpperCase())) {
                Log.e("getHexReverse", "输入的16进制是错误的");
                return hexString;
            }
        }
        int x = Integer.valueOf(hexString, 16);//16进制转化为10进制
        String y = Integer.toBinaryString(x);//将x转化为二进制
        StringBuffer sb = new StringBuffer();
        int size = hexString.length() * 4;//16进制 1位等于 2进制 4位  例如:f == 1111 4倍
        //如果位数不够 ,在前面补零
        for (int i = 0; i < size - y.length(); i++) {
            sb.append("0");
        }
        sb.append(y);
        char[] c = sb.toString().toCharArray();
        StringBuffer sb1 = new StringBuffer();
        for (int i = 0; i < c.length; i++) {
            sb1.append((c[i] == '0' ? 1 : 0));
        }
        int z = Integer.valueOf(sb1.toString(), 2);//2进制转化为10进制
        String result = Integer.toHexString(z).toUpperCase();
        //如果位数不够 ,在前面补零
        int resultLength = result.length();
        for (int i = 0; i < hexString.length() - resultLength; i++) {
            result = "0" + result;
        }
        Log.e("getHexReverse", "[2进制 原码:" + sb.toString() + " 反码:" + sb1.toString() + "] " +
                "[16进制 原码:" + hexString + " 反码:" + result + "]");
        return result;
    }


    /**
     * 获取BCC异或和校验值(异或校验法)
     *
     * @param hexString hex字符串
     * @return BCC异或和校验值(两位16进制字符串)
     */
    public static String getXorSum(String hexString) {
        hexString = changeToTwoBitHex(hexString);
        String[] b = hexString.split(" ");
        int a = 0;
        for (int i = 0; i < b.length; i++) {
            a = a ^ Integer.parseInt(b[i], 16);
        }
        if (a < 10) {
            StringBuffer sb = new StringBuffer();
            sb.append("0");
            sb.append(a);
            return sb.toString();
        }
        return Integer.toHexString(a);
    }

    /**
     * 用空格隔开每两位16进制值
     *
     * @param hexString hex字符串
     * @return hex字符串
     */
    public static String changeToTwoBitHex(String hexString) {
        String str = "";
        for (int i = 0; i < hexString.length(); i++) {
            if (i % 2 == 0) {
                str += " " + hexString.substring(i, i + 1);
            } else {
                str += hexString.substring(i, i + 1);
            }
        }
        return str.trim();
    }

    /**
     * Hex字符串转成字符串
     *
     * @param hexString Hex字符串
     * @return 字符串
     */
    public static String hexStringToString(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.replace(" ", "");
        byte[] baKeyword = new byte[hexString.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(hexString.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            hexString = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return hexString;
    }

    /**
     * 字符串转成Hex字符串
     *
     * @param string 字符串
     * @return Hex字符串
     */
    public static String stringToHexString(String string) {

        char[] chars = string.toCharArray();

        StringBuffer hex = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            hex.append(Integer.toHexString((int) chars[i]));
        }

        return hex.toString();
    }


    /**
     * Hex字符串转成byte数组
     * Hex长度为奇数最后一个字符会被舍去
     *
     * @param hexString Hex字符串
     * @return byte数组
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexString.length() / 2];
            int j = 0;
            for (int i = 0; i < hexString.length(); i += 2) {
                result[j++] = (byte) Integer.parseInt(hexString.substring(i, i + 2), 16);
            }
            return result;
        }
    }
}

你可能感兴趣的:(Android IC卡读写卡 RFID NFC BCC异或和校验值(异或校验法) 取反码 16进制 2进制转换工具)