Im trying to read a UTF8 file and convert it to CP850 ( to send to a printer device ). My test string is "ATIVAÇÃO"
A T I V A Ç Ã O
0x41 0x54 0x49 0x56 0x41 0xC3 0x87 C3 0x83 4F
My java code:
private static void printBytes(String s, String st) {
byte[] b_str = s.getBytes();
System .out.print(String.format("%-7s >>> ", st));
for (int i=0; i<s.length();i++)
System.out.print(String.format("%-7s ", s.charAt(i)));
System.out.println();
System .out.print(String.format("%-7s >>> ", st));
for (int i=0; i<b_str.length;i++)
System.out.print(String.format("0x%-5x ", (int)b_str[i] & 0xff));
System.out.println();
}
public static void main(String [] args) throws Exception, Exception {
String F="file.txt";
InputStreamReader input = new InputStreamReader(new FileInputStream(F));
BufferedReader in = new BufferedReader(input);
String strFILE;
String strCP850;
while ((strFILE = in.readLine()) != null) {
strFILE = strFILE.substring(3);
printBytes(strFILE, "ORI");
strCP850 = new String(strFILE.getBytes(), "CP850");
printBytes(strCP850, "CP850");
System.exit(0);
}
in.close();
}
The output:
ORI >>> A T I V A Ã ‡ Ã ƒ O
ORI >>> 0x41 0x54 0x49 0x56 0x41 0xc3 0x87 0xc3 0x83 0x4f
CP850 >>> A T I V A ? ç ? â O
CP850 >>> 0x41 0x54 0x49 0x56 0x41 0x3f 0xe7 0x3f 0xe2 0x4f
I was expecting "Ç" to be 0xc7 and "Ã" 0xc3, but the conversion result in a two byte char (like utf8...).
What im doing wrong?
Is there a way to do this (jdk 1.6)?