简体   繁体   English

特殊字符和Java字符串

[英]Special Characters and Java String

I have text string (Java String) which should contain 'gerald.o'leary' . 我有文本字符串(Java字符串),其中应包含'gerald.o'leary'

In realty it contains a control character in addition to the rest, 'c2'. 在房地产它包含除了休息,“C2”控制字符。 I have included the hex dump, please see image1.png for a hex dump. 我已经包括了十六进制转储,请参阅image1.png一个十六进制转储。

在此处输入图片说明

When I save it to the db and read it back by running SQL in a client and copy pasting into a hex editor I see c2 replaced by 3f, please see image2.png . 当我将其保存到数据库并通过在客户端中运行SQL并将其复制回十六进制编辑器中以将其读回时,我看到c2被3f取代了,请参见image2.png

在此处输入图片说明

I could have lived with it but for the fact when these two strings are compared in Java using String.equals(), false is returned. 我可以住在一起,但一个事实,当这两个字符串在Java中使用String.equals()相比,返回false。

Can somebody please explain what is going on here?! 有人可以解释一下这是怎么回事吗?

I don't know how you got the hex dump, but java strings are unicode strings so there is no 1:1 correspondence between characters and bytes. 我不知道您是怎么得到十六进制转储的,但是java字符串是unicode字符串,因此字符和字节之间没有1:1的对应关系。 I suspect your string contains unicode characters which can't be represented by single bytes, and your character handling (which assumes this to be the case) is buggy. 我怀疑您的字符串包含无法用单个字节表示的unicode字符,并且您的字符处理(假设是这种情况)有问题。

Did you check if your SQL DB can store UTF-8/Unicode characters (ie it's not ISO-8859-1, ASCII or similar)? 您是否检查您的SQL DB是否可以存储UTF-8 / Unicode字符(即不是ISO-8859-1,ASCII或类似字符)?

  • First output the String to the standard output to see if it's really containing the right character (? is fine in output, but ?? or 0xC2 0x92 = ´ is not). 首先将String输出到标准输出,以查看它是否确实包含正确的字符(?在输出中很好,但??或0xC2 0x92 =Â不是)。
  • Then check your database character set. 然后检查您的数据库字符集。 See its manual. 请参阅其手册。

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

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