# hexString code **Published by:** [sddcg](https://paragraph.com/@sddcg/) **Published on:** 2021-10-30 **URL:** https://paragraph.com/@sddcg/hexstring-code ## Content HexString——>byte 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); } byte——>String public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } byte——>hexString public String printHexString( byte[] b) { String a = ""; for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } a = a+hex; } return a; } ## Publication Information - [sddcg](https://paragraph.com/@sddcg/): Publication homepage - [All Posts](https://paragraph.com/@sddcg/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@sddcg): Subscribe to updates - [Twitter](https://twitter.com/jamsddcg): Follow on Twitter