From 57753dc2bc764d16904769ddd2bd8be35dec6beb Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期六, 27 七月 2024 16:27:16 +0800
Subject: [PATCH] 增加BCDUtil工具类
---
pipIrr-platform/文档/MybatisCodeHelper插件购买.docx | 0
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java | 103 --------------
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/BCDUtil.java | 273 +++++++++++++++++++++++++++++++++++++++
3 files changed, 273 insertions(+), 103 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/BCDUtil.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/BCDUtil.java
new file mode 100644
index 0000000..dfd7591
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/BCDUtil.java
@@ -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("瑙g爜BCD锛屼絾鏁版嵁闈濨CD鐮侊紒");
+ }
+ 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("瑙g爜BCD锛屼絾鏁版嵁锛�" + result + "闈濨CD鐮侊紒");
+ }
+ 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_);
+ }
+}
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java
index 6819327..2f0c7d6 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java
+++ b/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'};
-// /**
-// * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓�
-// *
-// * @param bytes 寰呰浆鎹yte[]
-// * @return 杩斿洖 杞崲鍚庣殑瀛楃涓�
-// */
-// public static String bytesToHex(byte[] bytes) {
-// //涓�涓猙yte涓�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);
-// }
-// /**
-// * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓�
-// *
-// * @param bytes 寰呰浆鎹yte[]
-// * @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] ;
-// }
-// //涓�涓猙yte涓�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);
-// }
-//
-// /**
-// * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓�
-// *
-// * @param bytes 寰呰浆鎹yte[]
-// * @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] ;
-// }
-// //涓�涓猙yte涓�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
diff --git "a/pipIrr-platform/\346\226\207\346\241\243/MybatisCodeHelper\346\217\222\344\273\266\350\264\255\344\271\260.docx" "b/pipIrr-platform/\346\226\207\346\241\243/MybatisCodeHelper\346\217\222\344\273\266\350\264\255\344\271\260.docx"
index ae83815..2ea5fe4 100644
--- "a/pipIrr-platform/\346\226\207\346\241\243/MybatisCodeHelper\346\217\222\344\273\266\350\264\255\344\271\260.docx"
+++ "b/pipIrr-platform/\346\226\207\346\241\243/MybatisCodeHelper\346\217\222\344\273\266\350\264\255\344\271\260.docx"
Binary files differ
--
Gitblit v1.8.0