@Test public void testByteArrayToHexString() throws Exception { class ExpOut { String out; String outWithSpaces; ExpOut(String o1, String o2) { out = o1; outWithSpaces = o2; } } Map<String, ExpOut> expectedResults = new HashMap<>(); expectedResults.put("", new ExpOut(null, null)); expectedResults.put("testa", new ExpOut("7465737461", "74 65 73 74 61")); expectedResults.put("testb", new ExpOut("7465737462", "74 65 73 74 62")); expectedResults.put("testc", new ExpOut("7465737463", "74 65 73 74 63")); expectedResults.put("testd", new ExpOut("7465737464", "74 65 73 74 64")); expectedResults.put("testtest", new ExpOut("7465737474657374", "74 65 73 74 74 65 73 74")); for (Map.Entry<String, ExpOut> entry : expectedResults.entrySet()) { String result = CommonUtils.byteArrayToHexString(entry.getKey().getBytes(), false); String resultWithSpaces = CommonUtils.byteArrayToHexString(entry.getKey().getBytes(), true); assertThat("Should return proper hex string", entry.getValue().out, is(result)); assertThat("Should return proper hex string with spaces", entry.getValue().outWithSpaces, is(resultWithSpaces)); } }
public static String byteArrayToHexString(byte[] bytes, boolean addSpacesBetweenBytes) { String hexString = null; if (bytes != null && bytes.length > 0) { char[] hexCharArray = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xFF; hexCharArray[i * 2] = HEX_ARRAY[v >>> 4]; hexCharArray[i * 2 + 1] = HEX_ARRAY[v & 0x0F]; } if (addSpacesBetweenBytes) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < hexCharArray.length; i += 2) { stringBuilder.append(hexCharArray[i]); stringBuilder.append(hexCharArray[i + 1]); if (i < hexCharArray.length - 2) { stringBuilder.append(" "); } hexString = stringBuilder.toString(); } } else { hexString = new String(hexCharArray); } } return hexString; }
CommonUtils { public static String byteArrayToHexString(byte[] bytes, boolean addSpacesBetweenBytes) { String hexString = null; if (bytes != null && bytes.length > 0) { char[] hexCharArray = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xFF; hexCharArray[i * 2] = HEX_ARRAY[v >>> 4]; hexCharArray[i * 2 + 1] = HEX_ARRAY[v & 0x0F]; } if (addSpacesBetweenBytes) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < hexCharArray.length; i += 2) { stringBuilder.append(hexCharArray[i]); stringBuilder.append(hexCharArray[i + 1]); if (i < hexCharArray.length - 2) { stringBuilder.append(" "); } hexString = stringBuilder.toString(); } } else { hexString = new String(hexCharArray); } } return hexString; } }
CommonUtils { public static String byteArrayToHexString(byte[] bytes, boolean addSpacesBetweenBytes) { String hexString = null; if (bytes != null && bytes.length > 0) { char[] hexCharArray = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xFF; hexCharArray[i * 2] = HEX_ARRAY[v >>> 4]; hexCharArray[i * 2 + 1] = HEX_ARRAY[v & 0x0F]; } if (addSpacesBetweenBytes) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < hexCharArray.length; i += 2) { stringBuilder.append(hexCharArray[i]); stringBuilder.append(hexCharArray[i + 1]); if (i < hexCharArray.length - 2) { stringBuilder.append(" "); } hexString = stringBuilder.toString(); } } else { hexString = new String(hexCharArray); } } return hexString; }  }
CommonUtils { public static String byteArrayToHexString(byte[] bytes, boolean addSpacesBetweenBytes) { String hexString = null; if (bytes != null && bytes.length > 0) { char[] hexCharArray = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xFF; hexCharArray[i * 2] = HEX_ARRAY[v >>> 4]; hexCharArray[i * 2 + 1] = HEX_ARRAY[v & 0x0F]; } if (addSpacesBetweenBytes) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < hexCharArray.length; i += 2) { stringBuilder.append(hexCharArray[i]); stringBuilder.append(hexCharArray[i + 1]); if (i < hexCharArray.length - 2) { stringBuilder.append(" "); } hexString = stringBuilder.toString(); } } else { hexString = new String(hexCharArray); } } return hexString; }  static boolean isLollipopOrHigher(); static boolean isMarshmallowOrHigher(); @TargetApi(23) static boolean isPermissionGranted(String permission, Activity activity); static boolean isValidBluetoothMacAddress(String bluetoothMacAddress); static void setMockedValue(Field originalField, Object parentObject, Object newValue); static boolean isNonEmptyString(String stringToCheck); static String byteArrayToHexString(byte[] bytes, boolean addSpacesBetweenBytes); }
CommonUtils { public static String byteArrayToHexString(byte[] bytes, boolean addSpacesBetweenBytes) { String hexString = null; if (bytes != null && bytes.length > 0) { char[] hexCharArray = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xFF; hexCharArray[i * 2] = HEX_ARRAY[v >>> 4]; hexCharArray[i * 2 + 1] = HEX_ARRAY[v & 0x0F]; } if (addSpacesBetweenBytes) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < hexCharArray.length; i += 2) { stringBuilder.append(hexCharArray[i]); stringBuilder.append(hexCharArray[i + 1]); if (i < hexCharArray.length - 2) { stringBuilder.append(" "); } hexString = stringBuilder.toString(); } } else { hexString = new String(hexCharArray); } } return hexString; }  static boolean isLollipopOrHigher(); static boolean isMarshmallowOrHigher(); @TargetApi(23) static boolean isPermissionGranted(String permission, Activity activity); static boolean isValidBluetoothMacAddress(String bluetoothMacAddress); static void setMockedValue(Field originalField, Object parentObject, Object newValue); static boolean isNonEmptyString(String stringToCheck); static String byteArrayToHexString(byte[] bytes, boolean addSpacesBetweenBytes);  }