我已经写了简单的转换代码从UTF-8转换为日语字符。
private static String convertUTF8ToShiftJ(String uft8Strg) {
String shftJStrg = null;
try {
byte[] b = uft8Strg.getBytes(UTF_8);
shftJStrg = new String(b, Charset.forName("SHIFT-JIS"));
logger.info("Converted to the string :" + shftJStrg);
} catch (Exception e) {
e.printStackTrace();
return uft8Strg;
}
return shftJStrg;
}但它给出了输出误差,
convertUTF8ToShiftJ START !!
uft8Strg=*** abc000.sh ����started�
*** abc000.sh å®�è¡�ä¸ï¼�executing...ï¼�
*** abc000.sh çµ�äº�ã��ï¼�endedã��ï¼�*有没有人知道我在哪里犯了错,或者需要一些额外的逻辑,这真的会很有帮助!
发布于 2018-08-15 15:35:12
你的String已经是一个String了,所以你的方法是“错误的”。UTF8是一种编码,它是byte[],可以在Java语言中转换为String。
它应该是:
private static byte[] convertUTF8ToShiftJ(byte[] uft8) {如果要将UTF8 byte[]转换为JIS byte[]
private static byte[] convertUTF8ToShiftJ(byte[] uft8) {
String s = new String(utf8, StandardCharsets.UTF_8);
return s.getBytes( Charset.forName("SHIFT-JIS"));
}稍后,可以通过mystring.getBytes(encoding)将String转换为byte[]
发布于 2018-08-15 15:58:42
看起来你对字符串编码有一个概念上的误解。有关示例Byte Encodings and Strings,请参阅。
将String从一种编码转换为另一种编码是没有意义的,因为String是独立于编码的。
但是,String可以用各种编码的字节数组来表示(例如UTF8或Shift-JIS)。因此,将UTF-8编码的字节数组转换为Shift-JIS编码的字节数组是有意义的。
private static byte[] convertUTF8ToShiftJ(byte[] utf8Bytes) throws IllegalCharsetNameException {
String s = new String(utf8Bytes, StandardCharsets.UTF_8);
byte[] shftJBytes = s.getBytes(Charset.forName("SHIFT-JIS"));
return shftJBytes;
}https://stackoverflow.com/questions/51854216
复制相似问题