| 97 | // Copied from GS3 main java code at GSDL3SRCHOME\src\java\org\greenstone/util\Misc.java |
| 98 | // Debugging function to print a string's non-basic chars in hex, so stringToHex on all non-basic and non-printable ASCII |
| 99 | // Dr Bainbridge said that printing anything with charCode over 128 in hex is okay, but I'd already made extra allowances for non-printable ASCII |
| 100 | // Based on https://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java |
| 101 | public static String debugUnicodeString(String str) { |
| 102 | String result = ""; |
| 103 | for(int i = 0; i < str.length(); i++) { |
| 104 | int charCode = str.codePointAt(i); // unicode codepoint / ASCII code |
| 105 | |
| 106 | // ASCII table: https://cdn.sparkfun.com/assets/home_page_posts/2/1/2/1/ascii_table_black.png |
| 107 | // If the unicode character code pt is less than the ASCII code for space and greater than for tilda, let's display the char in hex (x0000 format) |
| 108 | if((charCode >= 20 && charCode <= 126) || charCode == 9 || charCode == 10 || charCode == 13) { // space, tilda, TAB, LF, CR are printable, leave them in for XML element printing |
| 109 | result += str.charAt(i); |
| 110 | } else { |
| 111 | result += "x{" + String.format("%04x", charCode) + "}"; // looks like: x{4-char-codepoint} |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return result; |
| 116 | } |
| 117 | |