java - 有没有办法修复错误的编码字符串?

标签 java character-encoding

我通过消息代理 (Stomp) 获取此字符串:

João

这就是应该的样子:

João

有没有办法在 Java 中恢复它?! 谢谢!

最佳答案

U+00C3  Ã   c3 83   LATIN CAPITAL LETTER A WITH TILDE
U+00C2  Â   c3 82   LATIN CAPITAL LETTER A WITH CIRCUMFLEX
U+00A3  £   c2 a3   POUND SIGN
U+00E3  ã   c3 a3   LATIN SMALL LETTER A WITH TILDE

我无法确定这怎么可能是数据(编码)转换问题。有没有可能数据只是坏的?

如果数据不错,那么我们必须假设您误解了编码。我们不知道原始编码,除非您做一些不同的事情,否则 Java 的默认编码是 UTF-16。我不明白如何将以任何常见编码编码的 João 解释为 UTF-16 中的 Joã£o

可以肯定的是,我编写了这个 python 脚本,但没有找到匹配项。我不是完全确定它涵盖了所有编码,或者我没有遗漏一个极端情况,FWIW。

#!/usr/bin/env python                                                                                                                   
# -- coding: utf-8 --                                                                                                                   
import pkgutil
import encodings

good = u'João'
bad = u'João'

false_positives = set(["aliases"])

found = set(name for imp, name, ispkg in pkgutil.iter_modules(encodings.__path__) if not ispkg)
found.difference_update(false_positives)
print found


for x in found:
    for y in found:
        res = None
        try:
            res =  good.encode(x).decode(y)
            print res,x,y
        except:
            pass
        if not res is None:
            if res == bad:
                print "FOUND"
                exit(1)

关于java - 有没有办法修复错误的编码字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107691/

相关文章:

spring-boot - spring boot commandline runner 使用的是 Windows 默认字符编码

Python、Windows、Ansi——再次编码

java - ElementNotVisibleException 错误

java - 在android中对字符串数组列表进行排序

java - 如何从可能返回 null 的方法中分配变量?

php - 仅当从 PHP 插入时特殊字符才能正确显示

java - Oracle JRE 和 Dalvik JVM 上的不同结果

java - 如何向 jframe 添加多个矩形(尝试以简单的方式编码 2048)

java - Tomcat 性能问题

file - 使用文件 : protocol? 从 URL 读取的 API 的默认编码应该是什么