From 0e2034dd28f74fbf6fff0aef593b586a2dd9b634 Mon Sep 17 00:00:00 2001 From: liusuyi <13324259@qq.com> Date: 星期二, 14 十一月 2023 21:20:32 +0800 Subject: [PATCH] 修复bug --- src/main/java/com/ard/utils/other/ByteUtils.java | 29 ++++++++++++++++++++++++----- 1 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/ard/utils/other/ByteUtils.java b/src/main/java/com/ard/utils/other/ByteUtils.java index bda4b8c..0c11f68 100644 --- a/src/main/java/com/ard/utils/other/ByteUtils.java +++ b/src/main/java/com/ard/utils/other/ByteUtils.java @@ -30,13 +30,27 @@ * byte鏁扮粍杞腑鏂囧瓧绗︿覆 */ public static String bytesToStringZh(byte[] bytes) { - ByteBuffer buffer = ByteBuffer.wrap(bytes); // byteArray 鏄寘鍚瓧鑺傛暟缁勭殑鍙橀噺 + //ByteBuffer buffer = ByteBuffer.wrap(bytes); // byteArray 鏄寘鍚瓧鑺傛暟缁勭殑鍙橀噺 + //try { + // return new String(buffer.array(), "GBK"); + //} catch (UnsupportedEncodingException e) { + // e.printStackTrace(); + //} + //return ""; + String zhStr = ""; try { - return new String(buffer.array(), "GBK"); + int position = ByteUtils.findIndexOfDoubleZero(bytes); + if (position != -1) { + byte[] result = new byte[position]; + System.arraycopy(bytes, 0, result, 0, position); + zhStr = new String(result, "GBK"); + } else { + zhStr = new String(bytes, "GBK"); + } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } - return ""; + return zhStr; } /** @@ -67,8 +81,13 @@ * byte鏁扮粍杞暣鍨� */ public static int bytesToDecimal(byte[] byteArray) { - ByteBuffer buffer = ByteBuffer.wrap(byteArray); // byteArray 鏄寘鍚瓧鑺傛暟缁勭殑鍙橀噺 - return buffer.getInt(); + //ByteBuffer buffer = ByteBuffer.wrap(byteArray); // byteArray 鏄寘鍚瓧鑺傛暟缁勭殑鍙橀噺 + //return buffer.getInt(); + int decimalValue = 0; + for (int i = 0; i < byteArray.length; i++) { + decimalValue = (decimalValue << 8) | (byteArray[i] & 0xFF); + } + return decimalValue; } -- Gitblit v1.9.3