UTF-8字符串解码在java中,平假名符号变得不可读

UTF-8字符串解码在java中,平假名符号变得不可读,java,encoding,utf-8,decoding,Java,Encoding,Utf 8,Decoding,我创建以下代码来将具有给定字符集的字符串转换为其他字符串。请看下面 public static String convertCharsetOfTheString(String strToConvert,String targetCharsetName) throws UnsupportedEncodingException { CharsetDetector detector = new CharsetDetector(); detector.setText(st

我创建以下代码来将具有给定字符集的字符串转换为其他字符串。请看下面

public static String convertCharsetOfTheString(String strToConvert,String targetCharsetName) throws UnsupportedEncodingException {
        CharsetDetector detector = new CharsetDetector();
        detector.setText(strToConvert.getBytes());
        CharsetMatch detect = detector.detect();

        String currentCharsetName = detect.getName();

        Charset currentCharset=Charset.forName(currentCharsetName);

        Charset targetCharset=Charset.forName(targetCharsetName);

        ByteBuffer wrap = ByteBuffer.wrap(strToConvert.getBytes(targetCharsetName)); //.wrap(strToConvert.getBytes());

        CharBuffer decode = currentCharset.decode(wrap);


        ByteBuffer encode = targetCharset.encode(decode);


        return new String(encode.array(),targetCharsetName);

    }
对于一些符号,我有编码/解码错误。即平假名字母じ 变得无法阅读

我想这是因为平假名有3个字节而不是2个。但我不知道如何解决这个问题


有人知道如何修复它吗?

您如何调用此代码,例如,您正在尝试什么字符集?输出是什么?你期望得到什么样的结果?你在这里试图做的没有任何意义。字符串在逻辑上是一个字符序列,它没有编码(其内部表示是一个实现细节)。不同的编码只有当你把一个字符串转换成字节时才会起作用,反之亦然。2 Henry,但它在工作2 Josh Lee,我称之为不同的字符集。输出为UTF-8您仍然没有解释您正在尝试执行的操作、您正在传递给此函数的参数、您正在尝试使用的字符集,或者
getBytes()
正在使用的字符集。