“修复” Java中的字符串编码
我有一个使用 UTF-8 编码从 byte[]
数组创建的 String
。
但是,它应该是使用另一种编码(Windows-1252)创建的。
有没有办法将此字符串转换回正确的编码?
我知道如果您可以访问原始字节数组,这很容易做到,但就我而言,为时已晚,因为它是由闭源库提供的。
I have a String
created from a byte[]
array, using UTF-8 encoding.
However, it should have been created using another encoding (Windows-1252).
Is there a way to convert this String back to the right encoding?
I know it's easy to do if you have access to the original byte array, but it my case it's too late because it's given by a closed source library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于这是否可能似乎存在一些困惑,我认为我需要提供一个广泛的例子。
该问题声称(初始)输入是一个
byte[]
,其中包含 Windows-1252 编码数据。我将其称为byte[]
ib
(“初始字节”)。在这个例子中,我将选择德语单词“Bär”(意思是熊)作为输入:(
如果您的 JVM 不支持该编码,那么您可以使用 ISO-8859-1 代替,因为这三个字母(以及大多数其他)在这两种编码中处于相同位置)。
问题继续指出,其他一些代码(不在我们的影响范围内)已经使用 UTF-8 编码将
byte[]
转换为字符串(我将其称为String
是
“输入字符串”)。该String
是可用于实现我们目标的唯一输入(如果ib
可用,那将是微不足道的):这显然会产生错误的输出“B�”。
目标是生成
ib
(或该byte[]
的正确解码),且仅可用。 。现在有些人声称从
is
获取 UTF-8 编码字节将返回一个与初始数组具有相同值的数组:但这会返回 UTF-8 编码两个字符
B
和�
的组合,并且在重新解释为 Windows-1252 时肯定会返回错误的结果:此行产生输出“B�”,这完全是错误(如果初始数组包含非单词“Bür”,则结果也是相同的输出)。
因此在这种情况下您无法撤消该操作,因为一些信息丢失了。
事实上,在有情况下,这种错误编码是可以被撤销的。当所有可能的(或至少出现的)字节序列在该编码中都有效时,它更有可能起作用。由于 UTF-8 有几个字节序列根本不是有效值,因此您将会遇到问题。
As there seems to be some confusion on whether this is possible or not I think I'll need to provide an extensive example.
The question claims that the (initial) input is a
byte[]
that contains Windows-1252 encoded data. I'll call thatbyte[]
ib
(for "initial bytes").For this example I'll choose the German word "Bär" (meaning bear) as the input:
(If your JVM doesn't support that encoding, then you can use ISO-8859-1 instead, because those three letters (and most others) are at the same position in those two encodings).
The question goes on to state that some other code (that is outside of our influence) already converted that
byte[]
to a String using the UTF-8 encoding (I'll call thatString
is
for "input String"). ThatString
is the only input that is available to achieve our goal (ifib
were available, it would be trivial):This obviously produces the incorrect output "B�".
The goal would be to produce
ib
(or the correct decoding of thatbyte[]
) with onlyis
available.Now some people claim that getting the UTF-8 encoded bytes from that
is
will return an array with the same values as the initial array:But that returns the UTF-8 encoding of the two characters
B
and�
and definitely returns the wrong result when re-interpreted as Windows-1252:This line produces the output "B�", which is totally wrong (it is also the same output that would be the result if the initial array contained the non-word "Bür" instead).
So in this case you can't undo the operation, because some information was lost.
There are in fact cases where such mis-encodings can be undone. It's more likely to work, when all possible (or at least occuring) byte sequences are valid in that encoding. Since UTF-8 has several byte sequences that are simply not valid values, you will have problems.
我尝试了这个,由于某种原因它工作了
修复编码问题的代码(它不能完美工作,我们很快就会看到):
结果是:
这是另一个例子:
这是正在发生的事情以及为什么上面的技巧似乎有效:
现在,当我们尝试“反转”该过程时,会发生以下情况:
上面的编码修复代码可以工作,但对于以下字符失败:(
假设唯一的字符使用 Windows 1252 中的 1 字节字符):
它确实适用于一些字符,例如这些:
注意 - 我最初认为这与你的问题相关(并且因为我自己也在做同样的事情,所以我想我会分享我学到的东西),但看来我的问题有点不同的。也许这会帮助别人。
I tried this and it worked for some reason
Code to repair encoding problem (it doesn't work perfectly, which we will see shortly):
The results are:
Here's another example:
Here's what is happening and why the trick above seems to work:
Now, when we try to "reverse" the process, here is what happens:
The encoding fixing code above kind of works but fails for the following characters:
(Assuming the only characters used 1 byte characters from Windows 1252):
It does work for some of the characters, e.g. these:
NOTE - I originally thought this was relevant to your question (and as I was working on the same thing myself I figured I'd share what I've learned), but it seems my problem was slightly different. Maybe this will help someone else.
你想做的事情是不可能的。一旦有了 Java String,有关字节数组的信息就会丢失。您可能会幸运地进行“手动转换”。创建所有 windows-1252 字符及其到 UTF-8 的映射的列表。然后迭代字符串中的所有字符,将它们转换为正确的编码。
编辑:
正如评论者所说,这行不通。当您将 Windows-1252 字节数组转换为 UTF-8 时,您必然会遇到编码异常。 (请参阅此处和此处)。
What you want to do is impossible. Once you have a Java String, the information about the byte array is lost. You may have luck doing a "manual conversion". Create a list of all windows-1252 characters and their mapping to UTF-8. Then iterate over all characters in the string to convert them to the right encoding.
Edit:
As a commenter said this won't work. When you convert a Windows-1252 byte array as it if was UTF-8 you are bound to get encoding exceptions. (See here and here).
您可以使用此 教程
您需要的字符集应该在 rt.jar 中定义(根据 this )
You can use this tutorial
The charset you need should be defined in rt.jar (according to this)