As far as I understand, you are trying to store characters (from s1
) that contains non Latin-1 characters into a DB that only supports ISO-8859-1.
First, I agree with the others to say that it is a dirty idea.
Note that CP1252 is close from ISO-8859-1 (1 byte per character) and includes ™
Now, to anwser your question, I think you did the opposite..
You want to encode UTF-8 bytes into ISO-8859-1 :
String s2 = new String(s1.getBytes("UTF-8"), "ISO-8859-1");
This way, s2
is a characher String that, once encoded in ISO-8859-1, will return a byte array which may look like valid UTF-8 bytes.
To retrieve the original string, you would do
String s1 = new String(s2.getBytes("ISO-8859-1"),"UTF-8");
BUT WAIT ! When doing this, you hope that any byte can be decoded with ISO-8859-1 ..
and that your DB will accept such data. etc..
In fact, it is really unsure because officially, ISO-8859-1 doesn't have chars for any byte values.
For instance, from 80 to 9F.
Then,
byte[] b = { -97, -100, -128 };
System.out.println( new String(b,"ISO-8859-1") );
would display ???
However, in Java, s.getBytes("ISO-8859-1")
indeed restores the initial array.