Commit 70de8ee190c516b46f808a302678485fbc611e0a
1 parent
085c6e05
refactor: 乱码问题
1、字节数组转字符串乱码问题解决,兼容16进制 2、命令下发内容一致性问题。
Showing
1 changed file
with
17 additions
and
0 deletions
@@ -5,6 +5,7 @@ import org.apache.commons.lang3.ArrayUtils; | @@ -5,6 +5,7 @@ import org.apache.commons.lang3.ArrayUtils; | ||
5 | 5 | ||
6 | import java.io.*; | 6 | import java.io.*; |
7 | import java.nio.charset.Charset; | 7 | import java.nio.charset.Charset; |
8 | +import java.util.regex.Pattern; | ||
8 | 9 | ||
9 | /** | 10 | /** |
10 | * @Description :数据类型转换工具类 | 11 | * @Description :数据类型转换工具类 |
@@ -269,6 +270,22 @@ public class ByteUtils { | @@ -269,6 +270,22 @@ public class ByteUtils { | ||
269 | return sb.toString(); | 270 | return sb.toString(); |
270 | } | 271 | } |
271 | 272 | ||
273 | + private static final String messageRegular = "^[A-Za-z0-9!@#$%^&*()]+$"; | ||
274 | + public static String bytesToStr(byte[] bs){ | ||
275 | + String result = getString(bs, ByteUtils.UTF_8); | ||
276 | + if(!Pattern.compile(messageRegular).matcher(result).matches()){ | ||
277 | + result = bytesToHex(bs); | ||
278 | + } | ||
279 | + return result; | ||
280 | + } | ||
281 | + | ||
282 | + public static byte[] strToBytes(String str){ | ||
283 | + if(Pattern.compile(messageRegular).matcher(str).matches()){ | ||
284 | + return hexStr2Bytes(str); | ||
285 | + } | ||
286 | + return getBytes(str,ByteUtils.UTF_8); | ||
287 | + } | ||
288 | + | ||
272 | /** | 289 | /** |
273 | * 字节数组取前len个字节转16进制字符串 | 290 | * 字节数组取前len个字节转16进制字符串 |
274 | * | 291 | * |