Convert a hex-encoded String to binary data @param str A String containing the encoded data @return An array containing the binary data, or null if the string is invalid
(String str)
| 43 | * @return An array containing the binary data, or null if the string is invalid |
| 44 | */ |
| 45 | public static byte [] |
| 46 | fromString(String str) { |
| 47 | ByteArrayOutputStream bs = new ByteArrayOutputStream(); |
| 48 | byte [] raw = str.getBytes(); |
| 49 | for (int i = 0; i < raw.length; i++) { |
| 50 | if (!Character.isWhitespace((char)raw[i])) |
| 51 | bs.write(raw[i]); |
| 52 | } |
| 53 | byte [] in = bs.toByteArray(); |
| 54 | if (in.length % 2 != 0) { |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | bs.reset(); |
| 59 | DataOutputStream ds = new DataOutputStream(bs); |
| 60 | |
| 61 | for (int i = 0; i < in.length; i += 2) { |
| 62 | byte high = (byte) Base16.indexOf(Character.toUpperCase((char)in[i])); |
| 63 | byte low = (byte) Base16.indexOf(Character.toUpperCase((char)in[i+1])); |
| 64 | try { |
| 65 | ds.writeByte((high << 4) + low); |
| 66 | } |
| 67 | catch (IOException e) { |
| 68 | } |
| 69 | } |
| 70 | return bs.toByteArray(); |
| 71 | } |
| 72 | |
| 73 | } |
nothing calls this directly
no test coverage detected