UTF8 파일을 읽고 CP850으로 변환 (프린터 장치로 보내기)하려고합니다. 내 테스트 문자열은 "ATIVAÇÃO"입니다UTF8 파일 (메모장에서 작성)을 읽고 CP850 문자열로 변환
A T I V A Ç Ã O
0x41 0x54 0x49 0x56 0x41 0xC3 0x87 C3 0x83 4F
내 자바 코드 :
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();
}
출력 :
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
내가 expecting "C"이었다에는 0xC7와 "A"가 될 0xc3 변환 결과는 2 바이트 문자 (예 : utf8 ...)입니다.
무엇이 잘못 되었나요?
이렇게하는 방법이 있습니까 (jdk 1.6)?
을가 (대신 시스템 .out.print'의 및 String.format 아무것도 해결하지 않는 것이 (...))'System.out.format (...)'또는'System.out.printf (...) '를 사용할 수 있습니다. – Pshemo