我知道还有其他线程可以回答这个问题,但对我来说,它有点不同。
我有许多二进制文件,其中包含需要显示的不同类型的数据(ASCII、十六进制 ..)。
所以我通常显示 ASCII 值的方法是使用 StandardCharsets 类中包含的 ISO-8859-1。遗憾的是,它本身不支持 iso-8859.6 需要显示阿拉伯字符。 以下是我用于编码的方法:
第一种方法给我十六进制代码作为字符串:
public static String hexField(byte[] record,int offset,int length) {
StringBuilder s = new StringBuilder(length * 2);
int end = offset + length;
for (int i = offset; i < end; i++) {
int high_nibble = (record[i] & 0xf0) >>> 4;
int low_nibble = (record[i] & 0x0f);
s.append(hex_table[high_nibble]);
s.append(hex_table[low_nibble]);
}
return s.toString();
}
第二种方法:使用前一种方法显示ASCII字段:
private static String asciiField(byte[] record,int length) throws UnsupportedEncodingException {
String field = hexField(record,offset,length) ;
byte[] fieldByte = javax.xml.bind.DatatypeConverter.parseHexBinary(field);
return new String(fieldByte,StandardCharsets.ISO_8859_1).trim() ;
}
如何显示iso-8859.6编码的阿拉伯字符谢谢!