utf8 to unicode and unicode to utf8
804851Oct 12 2010 — edited Oct 13 2010hi...
how do you convert a utf8 encoding value (a String value not a file..i saw a lot of encoding conversion on the web but all of them are for files) to a default java encoding (UTF-16?) value ? ex: the euro sign ( € )
my goal is to be able to display the euro sign (or glyph of any unicode value) both in oracle and browser....
here's the story...
[scenario 1]
i have a jsp and it has textbox that accepts a value and saves it in the database (oracle) ... let's call my textbox "tb"... now if i type in the euro sign (i type in 20ac using an IME which then displays the euro sign) in the "tb" and hit submit and then i do a search (select *...from..) in oracle it showed me that the value i typed gets saved as:
â??¬
now if i try to go back to my jsp and try to search that same value (again by typing in 20ac using IME) it returns the correct row and it displays the euro just fine....
i tried looking at the hex equivalent of the euro that i typed:
00e2_0082_00ac <-- which means it's in utf-8 encoding
i also tried to do a dump in oracle and this is what i got:
Typ=1 Len=6: 0,e2,0,82,0,ac
[scenario 2]
i hardcoded the value in my java code instead of typing it in my jsp....so in my code i have this:
String string = "\u20ac";
and then did an insert in oracle...
hex value for that hardcoded string is:
20ac <--- which means it's already in unicode
a dump would give me the same:
Typ=1 Len=2: 20,ac
if i do a select (select *...from..) in the database (oracle) i can see the euro value displayed correctly
but if i do a search via my jsp i would just see a question mark:
?
so what i'm thinking to do is to convert the value from my jsp to unicode (java default) before saving it to oracle database
and if im going to retrieve the values to display it on my jsp i have to convert the unicode values to utf-8.
but my problem is i don't know how to do it..
i tried this:
//from utf8 to unicode
String string= (String) theForm.getValue("tb");
byte[] utf8 = string.getBytes("UTF-8");
String my_unicode = new String(utf8 , "UTF-16");
but it still isn't giving me the correct unicode equivalent
and i tried this to convert from unicode (the value i get from oracle) to utf-8 (so i could display the correct value on browser)
//from unicode to utf8
byte[] utf16 = raw_value.getBytes("UTF-16");
String my_utf8 = new String(utf16 );
but still no luck...
Notes:
Oracle is configured as unicode
My browser is set to utf-8 (using the: <meta http-equiv="content-type" content="text/html; charset=utf-8">)
Using Tomcat 6
Using JDK1.5.0_14
thank you so much in advance!
Edited by: 801848 on Oct 12, 2010 7:14 AM
Edited by: 801848 on Oct 12, 2010 4:11 PM