UTF-8 to Latin1 conversion

I have a customer who has a UTF-8 database and we use the HTTP plugin to post data to a third party which stores it in a Latin1 database.
Is there any way in Servoy, JavaScript or Java to transcode the UTF-8 data into Latin1 before we send it over?
I would be nice to be able to do this without needing to go to the commandline and use tools like iconv. :)

Thanks.

You should be able to do this in Java some way. Something like

inputString.getBytes(String charset)

should give you a byte array using the given charset.

Patrick thanks!
I will try that out.

Hi Robert,

Just watch out for the fact that you cannot use a JavaScript String but a JAVA String if you want to use the getBytes() method! So you will probably need to create a Java String out of your JavaScript String first, with something like:

var bytes = new java.lang.String(inputString).getBytes('Latin1');

And bytes will be a byte array.

yes, forgot to mention…

Thanks Patrick (T. and R.) :)

How do I get the resulting bytes back to a JS String (in Latin1) ?

You could try (all Java):

String str = new String (bytes);

If I understood you right, you have a “normal String” (UTF-8 internally) and want to convert that. Then you could try this

String result = new String (input.getBytes(), "Latin1")

To do this in a JavaScript method from within Servoy:

//go from a JavaScript string to a byte[]
var myString = 'hello';
var myStringsByteArray = new Packages.java.lang.String(myString).getBytes()
//To go from a byte[] to a JavaScript string in default encoding
var myString = 'hello';
var myStringsByteArray = new Packages.java.lang.String(myString).getBytes()
var myString2 = new Packages.java.lang.String(myStringsByteArray);
//To go from a byte[] to a JavaScript string in a specific encoding
var myString = 'hello';
var myStringsByteArray = new Packages.java.lang.String(myString).getBytes()
var myString2 = new Packages.java.lang.String(myStringsByteArray, 'ISO-8859-1');
//See http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc for more info on possible encodings

Paul

Thanks guys!
Will test/implement this next week.

pbakker:

//To go from a byte[] to a JavaScript string in a specific encoding

var myString = ‘hello’;
var myStringsByteArray = new Packages.java.lang.String(myString).getBytes()
var myString2 = new Packages.java.lang.String(myStringsByteArray, ‘ISO-8859-1’);
//See JDK 23 Documentation - Home for more info on possible encodings

I get the error that getBytes() is not a function.

Interestingly enough this does work on Servoy 5 (and I assume in 4.x) but doesn’t work in Servoy 3.5 (where I need to deploy it on)
All on the same machine and thus the same Java version.

So is this a Rhino issue ?

Just tried. It seems that the java String is converted to a JavaScript string and that’s why it fails. You could do that in a plugin…