Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding conversion in java

Is there any free java library which I can use to convert string in one encoding to other encoding, something like iconv? I'm using Java version 1.3.

like image 315
tropikalista Avatar asked Oct 23 '08 08:10

tropikalista


People also ask

How do you convert encoding in Java?

In order to convert a String into UTF-8, we use the getBytes() method in Java. The getBytes() method encodes a String into a sequence of bytes and returns a byte array. where charsetName is the specific charset by which the String is encoded into an array of bytes.

What is encoding in Java?

Encoding is a way to convert data from one format to another. String objects use UTF-16 encoding. The problem with UTF-16 is that it cannot be modified. There is only one way that can be used to get different encoding i.e. byte[] array. The way of encoding is not suitable if we get unexpected data.

Does Java use UTF-8 or UTF-16?

The native character encoding of the Java programming language is UTF-16.


1 Answers

You don't need a library beyond the standard one - just use Charset. (You can just use the String constructors and getBytes methods, but personally I don't like just working with the names of character encodings. Too much room for typos.)

EDIT: As pointed out in comments, you can still use Charset instances but have the ease of use of the String methods: new String(bytes, charset) and String.getBytes(charset).

See "URL Encoding (or: 'What are those "%20" codes in URLs?')".

like image 122
Jon Skeet Avatar answered Sep 29 '22 03:09

Jon Skeet