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_); } }