Alex Rivera | Logout

How to convert UTF-8 to US-Ascii in Java

Asked 2008-11-12T20:29:03.390
27

We have a system where customers, mainly European enter texts (in UTF-8) that has to be distributed to different systems, most of them accepting UTF-8, but now we must also distribute the texts to a US system which only accepts US-Ascii 7-bit

So now we'll need to translate all European characters to the nearest US-Ascii. Is there any Java libraries to help with this task?

Right now we've just started adding to a translation table, where Å (swedish AA)->A and so on and where we don't find any match for an entered character, we'll log it and replace with a question mark and try and fix that for the next release, but it seems very inefficient and somebody else must have done something similair before.

Edit
Report

1 Answer

0

There are some built in functions to do this. The main class involved is CharsetEncoder, which is part of the nio package. A simpler way is String.getBytes(Charset) that can be sent to a ByteArrayOutputStream.

answered 2008-11-12T20:34:09.950

Your Answer