-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
由字符长度改为起始位和结束位 原因:通过动态代理获取属性存在字典排序,非按照类属性内容顺序填充,存在问题
- Loading branch information
sunxudong
committed
Apr 22, 2020
1 parent
a362cc6
commit ae13fe5
Showing
13 changed files
with
541 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
package org.sheedon.demo; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Created by sheedon on 2017/9/28. | ||
*/ | ||
|
||
public class CharsUtils { | ||
/** | ||
* byte转hexString | ||
* | ||
* @param buffer 数据 | ||
* @param size 字符数 | ||
* @return | ||
*/ | ||
public static String bytesToHexString(final byte[] buffer, final int size) { | ||
StringBuilder stringBuilder = new StringBuilder(""); | ||
if (buffer == null || size <= 0) return null; | ||
for (int i = 0; i < size; i++) { | ||
String hex = Integer.toHexString(buffer[i] & 0xff); | ||
if (hex.length() < 2) stringBuilder.append(0); | ||
stringBuilder.append(hex); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
|
||
/** | ||
* hexString转byte | ||
* | ||
* @param hexString | ||
* @return | ||
*/ | ||
public static byte[] hexStringToBytes(String hexString) { | ||
if (hexString == null || hexString.equals("")) return null; | ||
hexString = hexString.toUpperCase(); | ||
int length = hexString.length() / 2; | ||
char[] hexChars = hexString.toCharArray(); | ||
byte[] d = new byte[length]; | ||
for (int i = 0; i < length; i++) { | ||
int pos = i * 2; | ||
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); | ||
} | ||
return d; | ||
} | ||
|
||
private static byte charToByte(char c) { | ||
return (byte) "0123456789ABCDEF".indexOf(c); | ||
} | ||
|
||
/** | ||
* 字符串转换为ASCII码 | ||
*/ | ||
public static String getAscii(String str) { | ||
StringBuilder sb = new StringBuilder(); | ||
byte[] bs = str.getBytes(); | ||
for (int i = 0; i < bs.length; i++) | ||
sb.append(toHex(bs[i])); | ||
return sb.toString(); | ||
} | ||
|
||
public static String toHex(int n) { | ||
StringBuilder sb = new StringBuilder(); | ||
if (n / 16 == 0) { | ||
return toHexUtil(n); | ||
} else { | ||
String t = toHex(n / 16); | ||
int nn = n % 16; | ||
sb.append(t).append(toHexUtil(nn)); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static String toHexUtil(int n) { | ||
String rt = ""; | ||
switch (n) { | ||
case 10: | ||
rt += "A"; | ||
break; | ||
case 11: | ||
rt += "B"; | ||
break; | ||
case 12: | ||
rt += "C"; | ||
break; | ||
case 13: | ||
rt += "D"; | ||
break; | ||
case 14: | ||
rt += "E"; | ||
break; | ||
case 15: | ||
rt += "F"; | ||
break; | ||
default: | ||
rt += n; | ||
} | ||
return rt; | ||
} | ||
|
||
/** | ||
* 十六进制转换字符串 | ||
* | ||
* @param hexStr | ||
* @return String 对应的字符串 | ||
*/ | ||
public static String hexStr2Str(String hexStr) { | ||
String str = "0123456789ABCDEF"; | ||
char[] hexs = hexStr.toCharArray(); | ||
byte[] bytes = new byte[hexStr.length() / 2]; | ||
int n; | ||
|
||
for (int i = 0; i < bytes.length; i++) { | ||
n = str.indexOf(hexs[2 * i]) * 16; | ||
n += str.indexOf(hexs[2 * i + 1]); | ||
bytes[i] = (byte) (n & 0xff); | ||
} | ||
return new String(bytes); | ||
} | ||
|
||
/** | ||
* bytes转换成十六进制字符串 | ||
* | ||
* @return String 每个Byte值之间空格分隔 | ||
*/ | ||
public static String byte2HexStr(byte[] b) { | ||
String stmp = ""; | ||
StringBuilder sb = new StringBuilder(""); | ||
for (int n = 0; n < b.length; n++) { | ||
stmp = Integer.toHexString(b[n] & 0xFF); | ||
sb.append((stmp.length() == 1) ? "0" + stmp : stmp); | ||
sb.append(" "); | ||
} | ||
return sb.toString().toUpperCase().trim(); | ||
} | ||
|
||
/** | ||
* bytes转换成十六进制字符串 | ||
* | ||
* @return String 每个Byte值之间空格分隔 | ||
*/ | ||
public static String byte2HexStr2(byte[] b) { | ||
String stmp = ""; | ||
StringBuilder sb = new StringBuilder(""); | ||
for (int n = 0; n < b.length; n++) { | ||
stmp = Integer.toHexString(b[n]); | ||
sb.append((stmp.length() == 1) ? "0" + stmp : stmp); | ||
sb.append(" "); | ||
} | ||
return sb.toString().toUpperCase().trim(); | ||
} | ||
|
||
/** | ||
* 利用正则表达式判断字符串是否是数字 | ||
* | ||
* @param str | ||
* @return | ||
*/ | ||
public static boolean isNumeric(String str) { | ||
Pattern pattern = Pattern.compile("[0-9]*"); | ||
Matcher isNum = pattern.matcher(str); | ||
if (!isNum.matches()) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public static boolean isNumber(char str) { | ||
return CharsUtils.isNumeric(String.valueOf(str)); | ||
} | ||
|
||
/** | ||
* bytes转换成字符串 | ||
*/ | ||
public static String byte2Str(byte[] b) { | ||
return hexStr2Str(byte2HexStr(b).replace(" ", "")); | ||
} | ||
|
||
public static String convertHexToString(String hex) { | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
StringBuilder temp = new StringBuilder(); | ||
|
||
//49204c6f7665204a617661 split into two characters 49, 20, 4c... | ||
for (int i = 0; i < hex.length() - 1; i += 2) { | ||
|
||
//grab the hex in pairs | ||
String output = hex.substring(i, (i + 2)); | ||
//convert hex to decimal | ||
int decimal = Integer.parseInt(output, 16); | ||
//convert the decimal to character | ||
sb.append((char) decimal); | ||
|
||
temp.append(decimal); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* 求校验和的算法 | ||
* | ||
* @param msg 需要求校验和的字节数组 | ||
* @return 校验和 | ||
*/ | ||
public static byte sumCheck(byte[] msg) { | ||
long sum = 0; | ||
|
||
/** 逐Byte添加位数和 */ | ||
for (byte byteMsg : msg) { | ||
if (byteMsg < 0) { | ||
sum += byteMsg & 0xff; | ||
} else { | ||
sum += byteMsg; | ||
} | ||
|
||
if (sum > 0xff) { | ||
sum &= 0xff; | ||
} | ||
} | ||
|
||
return (byte) (sum & 0xff); | ||
} | ||
|
||
public static String sumCheckToHexStr(byte[] msg) { | ||
byte b = sumCheck(msg); | ||
return byteToHex(b); | ||
} | ||
|
||
public static String byteToHex(byte b) { | ||
String hex = Integer.toHexString(b & 0xFF); | ||
if (hex.length() < 2) { | ||
hex = "0" + hex; | ||
} | ||
return hex.toUpperCase().trim(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.