List of utility methods to do Hex String to Byte Array Convert
byte[] | hexStr2ByteArr(String str) hex Str Byte Arr byte[] arrB = str.getBytes(); int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); return arrOut; ... |
byte[] | hexStr2Bytes(String src) hex Str Bytes int m = 0, n = 0; int l = src.length() / 2; System.out.println(l); byte[] ret = new byte[l]; for (int i = 0; i < l; i++) { m = i * 2 + 1; n = m + 1; ret[i] = uniteBytes(src.substring(i * 2, m), ... |
String | hexStr2Str(String hexStr) hex Str Str 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); ... |
byte[] | toByte(String hexString) to Byte int len = hexString.length() / 2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) result[i] = Integer.valueOf( hexString.substring(2 * i, 2 * i + 2), 16).byteValue(); return result; |
byte[] | toBytesFromHexString(String digits) to Bytes From Hex String if (digits == null) { return null; int length = digits.length(); if (length % 2 == 1) { throw new IllegalArgumentException("For input string: \"" + digits + "\""); length = length / 2; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { int index = i * 2; bytes[i] = (byte) (Short.parseShort( digits.substring(index, index + 2), 16)); return bytes; |
byte[] | hexStringToBytes(String s) hex String To Bytes if (s == null || s.length() == 0) return null; int len = s.length(); if (len % 2 != 0) { s = '0' + s; len++; byte[] data = new byte[len / 2]; ... |
byte[] | hexStringToBytes(String s) Converts a hex String to a byte array. byte[] ret; if (s == null) return null; int sz = s.length(); ret = new byte[sz / 2]; for (int i = 0; i < sz; i += 2) { ret[i / 2] = (byte) ((hexCharToInt(s.charAt(i)) << 4) | hexCharToInt(s .charAt(i + 1))); ... |
byte[] | decodeHex(String str) decode Hex str = str.toLowerCase(Locale.US); int len = str.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4) + Character .digit(str.charAt(i + 1), 16)); return data; ... |
String | fromHex(String hex) from Hex return new String(toByte(hex)); |
byte[] | hexStringToByteArray(String s) hex String To Byte Array int len = s.length(); byte[] ba = new byte[len / 2]; for (int i = 0; i < ba.length; i++) { int j = i * 2; int t = Integer.parseInt(s.substring(j, j + 2), 16); byte b = (byte) (t & 0xFF); ba[i] = b; return ba; |