Character Code Converter is a stand alone sample code that hepls in converting a java String object
into different character set formats example Unicode, UTF-8, DecimalCode, DecimalNCR Code, Hexadecimal code,
Hexadecimal NCR.
into different character set formats example Unicode, UTF-8, DecimalCode, DecimalNCR Code, Hexadecimal code,
Hexadecimal NCR.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | class CharacterCodeConverter { /** *This method is employed to convert the input string into a String of Decimal Code. * @param str: Input String which is to be converted into a String of Decimal Code. * @return: Decimal Code String. */ public String toDecimalCode(String str) { String outputString = "" ; int haut = 0 ; for ( int i = 0 ; i < str.length(); i++) { char b = str.charAt(i); if (b < 0 || b > 0xFFFF ) { outputString += "!error " + dec2hexCode(b) + "!" ; } if (haut != 0 ) { if ( 0xDC00 <= b && b <= 0xDFFF ) { outputString += dec2hexCode( 0x10000 + ((haut - 0xD800 ) << 10 ) + (b - 0xDC00 )) + "" ; haut = 0 ; continue ; } else { outputString += "!error " + dec2hexCode(haut) + "!" ; haut = 0 ; } } if ( 0xD800 <= b && b <= 0xDBFF ) { haut = b; } else { outputString += dec2hexCode(b) + "" ; } } return outputString; } private String dec2hexCode ( int textString) { int t = textString + 0 ; String temp = Integer.toString(t).toUpperCase()+ " " ; return temp; } /** *This method is employed to convert the input string into a String of Unicode. * @param str: Input String which is to be converted into a String of Unicode. * @return: Unicod String. */ public String convertToUnicode(String inputString) { String finalString = "" ; String hex= "" ; try { char [] charArray = inputString.toCharArray(); for ( int i= 0 ; i<charArray.length; i++) { hex = new String((Integer.toHexString(charArray[i])).getBytes( "Cp1252" ), "Cp1256" ); switch (hex.length()) { case 1 : finalString = finalString+ "\u000" ; break ; case 2 : finalString = finalString+ "\u00" ; break ; case 3 : finalString = finalString+ "\u0" ; break ; case 4 : finalString = finalString+ "\u" ; break ; default : System.out.println( hex+ " is too long to be a Character" ); } finalString = finalString+hex; } } catch (Exception e) { e.printStackTrace(); } return finalString; } /** *This method is employed to convert the input string into a Bytes Array of UTF-8. * @param str: Input String which is to be converted into a Byte Array of UTF-8. * @return: Byte Array of UTF-8. */ public byte [] convertToUtf8(String string) { byte [] utf8Bytes = null ; try { //String string = "abcu5639u563b"; utf8Bytes = string.getBytes( "UTF-8" ); } catch (Exception e) { e.printStackTrace(); } return utf8Bytes; } /** *This method is employed to convert the input string into a String of Decimal NCR. * @param str: Input String which is to be converted into a String of Decimal NCR. * @return: Decimal NCR String. */ public String toDecimalNCR(String str) { String outputString = "" ; int haut = 0 ; for ( int i = 0 ; i < str.length(); i++) { char b = str.charAt(i); if (b < 0 || b > 0xFFFF ) { outputString += "!error " + dec2hex(b) + "!" ; } if (haut != 0 ) { if ( 0xDC00 <= b && b <= 0xDFFF ) { outputString += dec2hex( 0x10000 + ((haut - 0xD800 ) << 10 ) + (b - 0xDC00 )) + "" ; haut = 0 ; continue ; } else { outputString += "!error " + dec2hex(haut) + "!" ; haut = 0 ; } } if ( 0xD800 <= b && b <= 0xDBFF ) { haut = b; } else { outputString += dec2hex(b) + "" ; } } return outputString; } private String dec2hex ( int textString) { int t = textString + 0 ; String temp = "&#" +Integer.toString(t).toUpperCase()+ ";" ; return temp.trim(); } /** *This method is employed to convert the input string into a String Array of UTF-8. * @param str: Input String which is to be converted into a String Array of UTF-8. * @return: String Array of UTF-8. */ public String[] convertToUTF8(String string) { String[] sUTF8= null ; //new sHexBytes[100]; byte [] bUTF8Bytes = null ; try { //String string = "abcu5639u563b"; bUTF8Bytes = string.getBytes( "UTF-8" ); sUTF8=printUTF8(bUTF8Bytes); } catch (Exception e) { e.printStackTrace(); } return sUTF8; } private String[] printUTF8( byte [] array) { int l=array.length; String[] temp = new String[l]; for ( int k = 0 ; k < array.length; k++) { temp[k]= new String(); temp[k] =byteToHex(array[k]); temp[k].trim(); } return temp; } /** *This method is employed to convert the input string into a String Array of Hexadecimal NCR Code. * @param string: Input String which is to be converted into a String Array of Hexadecimal NCR Code. * @return: String Array of Hexadecimal NCR Code. */ public String[] convertToHexNCR(String string) { String[] sHexBytes= null ; byte [] hexBytes = null ; try { hexBytes = string.getBytes( "UTF-8" ); sHexBytes=printHexNCR(hexBytes); } catch (Exception e) { e.printStackTrace(); } return sHexBytes; } private String[] printHexNCR( byte [] array) { int l=array.length; String[] temp = new String[l]; for ( int k = 0 ; k < array.length; k++) { temp[k]= new String(); temp[k] = "&#x" + byteToHex(array[k])+ ";" ; temp[k].trim(); } return temp; } /** *This method is employed to convert the input string into a String Array of Hexadecimal Code. * @param string: Input String which is to be converted into a String Array of Hexadecimal Code. * @return: String Array of Hexadecimal Code. */ public String[] convertToHEX(String string) { String[] sHexBytes= null ; byte [] hexBytes = null ; try { hexBytes = string.getBytes( "UTF-8" ); sHexBytes=printHex(hexBytes); } catch (Exception e) { e.printStackTrace(); } return sHexBytes; } private String[] printHex( byte [] array) { int l=array.length; String[] temp = new String[l]; for ( int k = 0 ; k < array.length; k++) { temp[k]= new String(); temp[k] = "0x" + byteToHex(array[k]); temp[k].trim(); } return temp; } ///////////////////////////////////// private String byteToHex( byte b) { // Returns hex String representation of byte b char hexDigit[] = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' }; char [] array = { hexDigit[(b >> 4 ) & 0x0f ], hexDigit[b & 0x0f ] }; return new String(array); } private String charToHex( char c) { // Returns hex String representation of char c byte hi = ( byte ) (c >>> 8 ); byte lo = ( byte ) (c & 0xff ); return byteToHex(hi) + byteToHex(lo); } } //class |