How can I convert a array of bytes
to String
without conversion?.
I tried:
String doc=new String( bytes);
But the doc file is not the same than the bytes (the bytes are binary information). For example:
String doc=new String( bytes);
byte[] bytes2=doc.getBytes();
bytes
and bytes2
are different.
PS: UTF-8 Does not work because it convert some bytes in different values. I tested and it does not work.
PS2: And no, I don't want BASE64
.
Given a Byte value in Java, the task is to convert this byte value to string type. One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable.
Double is a higher datatype compared to byte. Therefore, double value will not be converted into byte implicitly, you need to convert it using the cast operator.
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.
You need to specify the encoding you want e.g. for UTF-8
String doc = ....
byte[] bytes = doc.getBytes("UTF-8");
String doc2 = new String(bytes, "UTF-8");
doc
and doc2
will be the same.
To decode a byte[]
you need to know what encoding was used to be sure it will decode correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With