Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: convert UTF8 String to byte array in another encoding

I have UTF8 encoded String, but I need to post parameters to Runtime process in cp1251. How can I decode String or byte array?

I need smth like:.bytesInCp1251 = encodeTo(stringInUtf8, "cp1251");


Thanks to all! This is my own solution:

OutputStreamWriter writer = new OutputStreamWriter(out, "cp1251");
writer.write(s);
like image 640
Pavel Vyazankin Avatar asked Oct 26 '10 09:10

Pavel Vyazankin


People also ask

Can we convert string to byte array in Java?

We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument. Here is a simple program showing how to convert String to byte array in java.

How do I change the encoding of a string in Java?

Strings are immutable in Java, which means we cannot change a String character encoding. To achieve what we want, we need to copy the bytes of the String and then create a new one with the desired encoding.

What is getBytes UTF-8?

UTF stands for Unicode Transformation Format. The '8' signifies that it allocates 8-bit blocks to denote a character. The number of blocks needed to represent a character varies from 1 to 4. In order to convert a String into UTF-8, we use the getBytes() method in Java.

What is getBytes method in Java?

getBytes() This function takes no arguments and used the default charset to encode the string into bytes. getbytes() function in java is used to convert a string into a sequence of bytes and returns an array of bytes. Syntax: public byte[] getBytes()


2 Answers

There is no such thing as an "UTF8 encoded String" in Java. Java Strings use UTF-16 internally, but should be seen as an abstraction without a specific encoding. If you have a String, it's already decoded. If you want to encode it, use string.getBytes(encoding). If you original data is UTF-8, you have to take that into account when you convert that data from bytes to String.

like image 87
Michael Borgwardt Avatar answered Oct 01 '22 00:10

Michael Borgwardt


byte[] bytesInCp1251 = stringInUtf8.getBytes("cp1251");
like image 30
William Avatar answered Oct 01 '22 01:10

William