Commit e09a4395 by 谢海旭

实现 Ocpp16Helper.strToAscii 方法

1 parent b3abfe49
...@@ -175,27 +175,26 @@ public class Ocpp16Helper { ...@@ -175,27 +175,26 @@ public class Ocpp16Helper {
return bcdBytes; return bcdBytes;
} }
public static byte[] strToAscii(String input, byte[] bcdBytes) { /**
int len = input.length(); * 字符串 转ascii字节数组
int mod = len % 2; * 不足指定长度,默认后面补零
*
if (mod != 0) { * @param str 字符串
input = "0" + input; // 补齐偶数位 * @param asciiBytes ascii字节数组(长度限定)
len = input.length(); * @return
*/
public static byte[] strToAscii(String str, byte[] asciiBytes) {
if (str.length() > asciiBytes.length) {
throw new IllegalArgumentException("The str length is greater than the asciiBytes length");
} }
for (int i = 0; i < len; i += 2) { char[] charArray = str.toCharArray();
char highChar = input.charAt(i); int length = charArray.length;
char lowChar = input.charAt(i + 1); for (int i = 0; i < length; i++) {
asciiBytes[i] = (byte) charArray[i];
int high = Character.digit(highChar, 10);
int low = Character.digit(lowChar, 10);
byte bcdByte = (byte) ((high << 4) | low);
bcdBytes[i / 2] = bcdByte;
} }
return bcdBytes; return asciiBytes;
} }
public static byte[] binToBytes(String binaryString) { public static byte[] binToBytes(String binaryString) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!