Special Characters and Java String
-
12-12-2019 - |
Question
I have text string (Java String) which should contain 'gerald.o'leary'
.
In realty it contains a control character in addition to the rest, 'c2'. I have included the hex dump, please see image1.png
for a hex dump.
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
.
I could have lived with it but for the fact when these two strings are compared in Java using String.equals(), false is returned.
Can somebody please explain what is going on here?!
Solution
Did you check if your SQL DB can store UTF-8/Unicode characters (i.e. it's not ISO-8859-1, ASCII or similar)?
- 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).
- Then check your database character set. See its manual.
OTHER TIPS
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. 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.