From 4513ef24bf9b188c2a77d6ce94f1a6b7e9ebf0e6 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期日, 27 四月 2025 20:40:19 +0800 Subject: [PATCH] fix(irrigatePlan): 修正灌溉计划开始时间逻辑 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/BCDUtil.java | 273 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 273 insertions(+), 0 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_); + } +} -- Gitblit v1.8.0