liurunyu
2024-07-27 57753dc2bc764d16904769ddd2bd8be35dec6beb
增加BCDUtil工具类
1个文件已添加
2个文件已修改
376 ■■■■ 已修改文件
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/BCDUtil.java 273 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java 103 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/文档/MybatisCodeHelper插件购买.docx 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/BCDUtil.java
New file
@@ -0,0 +1,273 @@
package com.dy.common.util;
/**
 * @Author: liurunyu
 * @Date: 2024/7/27 15:50
 * @Description
 */
public class BCDUtil {
    /**
     *
     * @param num
     * @return
     */
    public static byte[] int2BCD_LE(int num){
        int len =  (int) Math.log10(num) + 1 ;
        byte[] bs = null ;
        if(len % 2 == 0){
            bs = new byte[len / 2] ;
        }else{
            bs = new byte[len / 2 + 1] ;
        }
        int index = 0 ;
        while(num > 0){
            int yu = num % 100 ;
            bs[index++] = (byte)(((yu/10) << 4) + (yu%10)) ;
            num = num / 100 ;
        }
        return bs ;
    }
    /**
     *
     * @param num
     * @return
     */
    public static byte[] int2BCD_BE(int num){
        int len =  (int) Math.log10(num) + 1 ;
        byte[] bs = null ;
        if(len % 2 == 0){
            bs = new byte[len / 2] ;
        }else{
            bs = new byte[len / 2 + 1] ;
        }
        int index = bs.length - 1 ;
        while(num > 0){
            int yu = num % 100 ;
            bs[index--] = (byte)(((yu/10) << 4) + (yu%10)) ;
            num = num / 100 ;
        }
        return bs ;
    }
    /**
     *
     * @param num
     * @return
     */
    public static byte[] long2BCD_LE(long num){
        int len =  (int) Math.log10(num) + 1 ;
        byte[] bs = null ;
        if(len % 2 == 0){
            bs = new byte[len / 2] ;
        }else{
            bs = new byte[len / 2 + 1] ;
        }
        int index = 0 ;
        while(num > 0){
            long yu = num % 100 ;
            bs[index++] = (byte)(((yu/10) << 4) + (yu%10)) ;
            num = num / 100 ;
        }
        return bs ;
    }
    /**
     *
     * @param num
     * @return
     */
    public static byte[] long2BCD_BE(long num){
        int len =  (int) Math.log10(num) + 1 ;
        byte[] bs = null ;
        if(len % 2 == 0){
            bs = new byte[len / 2] ;
        }else{
            bs = new byte[len / 2 + 1] ;
        }
        int index = bs.length - 1 ;
        while(num > 0){
            long yu = num % 100 ;
            bs[index--] = (byte)(((yu/10) << 4) + (yu%10)) ;
            num = num / 100 ;
        }
        return bs ;
    }
    /**
     *
     * @param numStr
     * @return
     */
    public static byte[] string2BCD_LE(String numStr) throws Exception{
        if (numStr == null || !numStr.matches("\\d*")) {
            throw new Exception("数字转成BCD编码时出错,不是合法数字:" + numStr, null);
        }
        int numLen = numStr.length() ;
        int byteLen = 0 ;
        if(numLen % 2 == 0){
            byteLen = numLen / 2 ;
        }else{
            byteLen = numLen / 2 + 1 ;
        }
        int[] tmpInts = new int[byteLen * 2];
        int index = numStr.length() - 1;
        for (int i = 0; i <= tmpInts.length - 1 && index >= 0; i++, index--) {
            tmpInts[i] = numStr.charAt(index) - '0';
        }
        byte[] bs = new byte[byteLen] ;
        for (int i = 0, j = 0; i < byteLen; i++, j++) {
            bs[i] = (byte) (tmpInts[2 * j + 1] * 16 + tmpInts[2 * j]);
        }
        return bs ;
    }
    /**
     *
     * @param numStr
     * @return
     */
    public static byte[] string2BCD_BE(String numStr) throws Exception{
        if (numStr == null || !numStr.matches("\\d*")) {
            throw new Exception("数字转成BCD编码时出错,不是合法数字:" + numStr, null);
        }
        int numLen = numStr.length() ;
        int byteLen = 0 ;
        if(numLen % 2 == 0){
            byteLen = numLen / 2 ;
        }else{
            byteLen = numLen / 2 + 1 ;
        }
        int[] tmpInts = new int[byteLen * 2];
        int index = numStr.length() - 1;
        for (int i = tmpInts.length - 1; i >= 0 && index >= 0; i--, index--) {
            tmpInts[i] = numStr.charAt(index) - '0';
        }
        byte[] bs = new byte[byteLen] ;
        for (int i = 0, j = 0; i < byteLen; i++, j++) {
            bs[i] = (byte) (tmpInts[2 * j] * 16 + tmpInts[2 * j + 1]);
        }
        return bs ;
    }
    /**
     *
     * @param bs
     * @return
     */
    public static int BCD2Int_LE(byte[] bs){
        int num = 0 ;
        int multiple = 1 ;
        for(int i = 0; i < bs.length; i++){
            num += (((bs[i] >> 4) * 10) + (bs[i] & 0xF)) * multiple ;
            multiple = multiple * 100 ;
        }
        return num ;
    }
    /**
     *
     * @param bs
     * @return
     */
    public static int BCD2Int_BE(byte[] bs){
        int num = 0 ;
        int multiple = 1 ;
        for(int i = bs.length-1; i >= 0 ; i--){
            num += (((bs[i] >> 4) * 10) + (bs[i] & 0xF)) * multiple ;
            multiple = multiple * 100 ;
        }
        return num ;
    }
    /**
     *
     * @param bs
     * @return
     */
    public static long BCD2Long_LE(byte[] bs){
        long num = 0 ;
        long multiple = 1 ;
        for(int i = 0; i < bs.length; i++){
            num += (((bs[i] >> 4) * 10) + (bs[i] & 0xF)) * multiple ;
            multiple = multiple * 100 ;
        }
        return num ;
    }
    /**
     *
     * @param bs
     * @return
     */
    public static long BCD2Long_BE(byte[] bs){
        long num = 0 ;
        long multiple = 1 ;
        for(int i = bs.length-1; i >= 0 ; i--){
            num += (((bs[i] >> 4) * 10) + (bs[i] & 0xF)) * multiple ;
            multiple = multiple * 100 ;
        }
        return num ;
    }
    /**
     *
     * @param bs
     * @return
     */
    public static String BCD2String_LE(byte[] bs) throws Exception{
        StringBuilder sb = new StringBuilder();
        for (int i = (bs.length - 1); i >= 0; i--) {
            int value = (bs[i] + 256) % 256;
            sb.append((char) (value / 16 + '0')).append((char) (value % 16 + '0'));
        }
        String result = sb.toString();
        if (!result.matches("\\d*")) {
            throw new Exception("解码BCD,但数据非BCD码!");
        }
        return result;
    }
    /**
     *
     * @param bs
     * @return
     */
    public static String BCD2String_BE(byte[] bs) throws Exception{
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bs.length ; i++) {
            int value = (bs[i] + 256) % 256;
            sb.append((char) (value / 16 + '0')).append((char) (value % 16 + '0'));
            value++;
        }
        String result = sb.toString();
        if (!result.matches("\\d*")) {
            throw new Exception("解码BCD,但数据(" + result + "非BCD码!");
        }
        return result;
    }
    public static void main(String[] args) throws Exception {
        int num = 1234567;
        byte[] bs = int2BCD_LE(num) ;
        int num_ =BCD2Int_LE(bs) ;
        System.out.println(num_);
        bs = int2BCD_BE(num) ;
        num_ = BCD2Int_BE(bs);
        System.out.println(num_);
        String str = "12345678901234567890" ;
        bs = string2BCD_LE(str) ;
        String str_ = BCD2String_LE(bs) ;
        System.out.println(str_);
        bs = string2BCD_BE(str) ;
        str_ = BCD2String_BE(bs) ;
        System.out.println(str_);
    }
}
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java
@@ -1059,109 +1059,6 @@
    }
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
//    /**
//     * å°†byte[]转换为16进制字符串
//     *
//     * @param bytes å¾…转换byte[]
//     * @return è¿”回 è½¬æ¢åŽçš„字符串
//     */
//    public static String bytesToHex(byte[] bytes) {
//        //一个byte为8位,可用两个十六进制位标识
//        char[] buf = new char[bytes.length * 2];
//        int a = 0;
//        int index = 0;
//        for (byte b : bytes) { // ä½¿ç”¨é™¤ä¸Žå–余进行转换
//            if (b < 0) {
//                a = 256 + b;
//            } else {
//                a = b;
//            }
//
//            buf[index++] = HEX_CHAR[a / 16];
//            buf[index++] = HEX_CHAR[a % 16];
//        }
//        return new String(buf);
//    }
//    /**
//     * å°†byte[]转换为16进制字符串
//     *
//     * @param bytes å¾…转换byte[]
//     * @return è¿”回 è½¬æ¢åŽçš„字符串
//     */
//    public static String bytesToHex_BE(byte[] bytes, int startIndex, int endIndex) {
//        byte[] bs = new byte[endIndex - startIndex + 1] ;
//        byte j = 0 ;
//        for(int i = startIndex; i <= endIndex; i++){
//            bs[j++] = bytes[i] ;
//        }
//        //一个byte为8位,可用两个十六进制位标识
//        char[] buf = new char[bs.length * 2];
//        int a = 0;
//        int index = 0;
//        for (byte b : bs) { // ä½¿ç”¨é™¤ä¸Žå–余进行转换
//            if (b < 0) {
//                a = 256 + b;
//            } else {
//                a = b;
//            }
//
//            buf[index++] = HEX_CHAR[a / 16];
//            buf[index++] = HEX_CHAR[a % 16];
//        }
//        return new String(buf);
//    }
//
//    /**
//     * å°†byte[]转换为16进制字符串
//     *
//     * @param bytes å¾…转换byte[]
//     * @return è¿”回 è½¬æ¢åŽçš„字符串
//     */
//    public static String bytesToHex_LE(byte[] bytes, int startIndex, int endIndex) {
//        byte[] bs = new byte[endIndex - startIndex + 1] ;
//        byte j = 0 ;
//        for(int i = endIndex; i >= startIndex; i--){
//            bs[j++] = bytes[i] ;
//        }
//        //一个byte为8位,可用两个十六进制位标识
//        char[] buf = new char[bs.length * 2];
//        int a = 0;
//        int index = 0;
//        for (byte b : bs) { // ä½¿ç”¨é™¤ä¸Žå–余进行转换
//            if (b < 0) {
//                a = 256 + b;
//            } else {
//                a = b;
//            }
//
//            buf[index++] = HEX_CHAR[a / 16];
//            buf[index++] = HEX_CHAR[a % 16];
//        }
//        return new String(buf);
//    }
//
//    /**
//     * å°†16进制字符串转换为byte[]
//     *
//     * @param str å¾…转换字符串
//     * @return è¿”回 è½¬æ¢åŽçš„byte[]
//     */
//    public static byte[] hexToBytes(String str) {
//        if (str == null || "".equals(str.trim())) {
//            return new byte[0];
//        }
//
//        byte[] bytes = new byte[str.length() / 2];
//        for (int i = 0; i < str.length() / 2; i++) {
//            String subStr = str.substring(i * 2, i * 2 + 2);
//            bytes[i] = (byte) Integer.parseInt(subStr, 16);
//        }
//
//        return bytes;
//    }
    /**
     * Convert char to byte
     * @param c char
pipIrr-platform/Îĵµ/MybatisCodeHelper²å¼þ¹ºÂò.docx
Binary files differ