views:

3367

answers:

4

I need to convert a UTF-8 trademark sign to a ISO Latin 1, and save it into database, which is also ISO Latin 1 encoded.

How can I do that in java?

I've tried something like

String s2 = new String(s1.getBytes("ISO-8859-1"), "utf-8");

but it seems not work as I expected.

+5  A: 

A string in Java is always in Unicode (UTF-16, effectively). Conversions are only necessary when you're trying to go from text to a binary encoding or vice versa.

What's the character involved? Are you sure it's even present in ISO Latin 1? If it is, I'd expect that character to be stored by your database without any problem. There's no such thing as a "UTF-8 trademark sign". You could have "the bytes representing the trademark sign UTF-8 encoded" but that would be a byte array, not a string.

EDIT: If you mean the Unicode trademark character U+2122, that's outside the range of ISO-Latin-1. There's the registered trademark character U+00AE, which isn't the same thing (either in appearance or in legal meaning, IIRC) but may be better than nothing - if you want to use that then just use:

string replaced = original.replace('\u2122', '\u00ae');
Jon Skeet
But <®> and <™> have quite different meanings.
Joachim Sauer
Hence "isn't the same thing (either in appearance or legal meaning"
Jon Skeet
+2  A: 

Have a look at http://stackoverflow.com/questions/285228/how-to-convert-utf-8-to-us-ascii-in-java Not exactly a duplicate, but similar.

Paul Tomblin
+2  A: 
  1. Read what Jon Skeet told you. The Code you posted is rubbish (it takes the UTF-8 encoded form of your String and interprets it as if it were ISO-8859-1, this accomplishes nothing useful).
  2. The ISO-8859-1 encoding (a.k.a Latin1) doesn't contain the Trademark character "™".
Joachim Sauer
A: 

can u give me answer how to conver forgien chaterne in to letin or simple chaareter in php ? how to convert ecute chater to noaecute chatater in php plez give me suggesation

khushi patel