最近正在做一个生成RTF格式报表的程序,在过程中遇到了一个小问题:
如何将中文(包括:简体、繁体)转换为RTF格式的文本?
经过研究,可以使用这段代码解决问题:
public String getAscii(char charvalue) {
byte[] bytes = (String.valueOf(charvalue)).getBytes();
if (bytes.length == 1) {
return String.valueOf(bytes[0]);
}
if (bytes.length == 2) {
StringBuffer buffer = new StringBuffer();
int hightByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];
int b1 = hightByte/16;
int b2 = hightByte%16;
int b3 = lowByte/16;
int b4 = lowByte%16;
buffer.append(Integer.toHexString(b1));
buffer.append(Integer.toHexString(b2));
buffer.append(Integer.toHexString(b3));
buffer.append(Integer.toHexString(b4));
return buffer.toString();
}
return "0000";
}
这段代码适用于所有需要将中文转换为ASCII的地方。