简体   繁体   English

Java 不解码传递的字符串(使用 unicode)

[英]Java doesn't decode passed string (with unicode)

I am creating string (by concatanation of input string and some predefined text), save it into database and pass it to the following method:我正在创建字符串(通过输入字符串和一些预定义文本的串联),将其保存到数据库中并将其传递给以下方法:

public String decodeUTF(String inputString) {
    byte[] bytes =  StringUtils.getBytesUtf8(inputString);
    return StringUtils.newStringUtf8(bytes);
}

When i call (and send result to front(android)) :当我打电话(并将结果发送到前台(android))时:

decodeUTF("Emoji example: \uD83D\uDE04");

it works (shows smilies) .它有效(显示笑脸)。

When i call当我打电话

decodeUTF(sameStringFromDb);

it passes whole string whithout conversion.它在没有转换的情况下传递整个字符串。

In Java source code, \? is an escape code : The compiler replaces it with one code unit .在 Java 源代码中, \?是一种转义码:编译器将其替换为一个代码单元

If you see \? in your database, it's not an escape code, it's the sequence of six individual characters '\\' 'u' 'D' '8' '3' 'D'.如果您在数据库中看到\? ,则它不是转义码,而是六个单独字符'\\' 'u' 'D' '8' '3' 'D' 的序列。

What's the right way to fix this and make sure you get the same output anyway?解决此问题并确保获得相同输出的正确方法是什么?

One thing you must ask is why did the text "\?" get to the database in the first place.您必须问的一件事是为什么文本“\?”首先到达数据库。 Text stored in a database should not be mangled in this way.存储在数据库中的文本不应以这种方式被破坏。 It sounds like there is a bug at the data entry.听起来数据输入处存在错误。

If there's no way to fix the data entry, and you want to replace the text "\?" with a single character just like the Java compiler would, that has already been covered in other questions, see for example Convert escaped Unicode character back to actual character如果没有办法修复数据输入,并且您想像 Java 编译器那样用单个字符替换文本“\?”,这已经在其他问题中涵盖,请参见例如将转义的 Unicode 字符转换回实际性格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM