I am working on a proxy server. I am getting data in byte[]
which I convert into a String
to perform certain operations. Now when i convert this new String
back into a byte[]
it causes unknown problems.
So mainly its like I need to know how to correctly convert abyte[]
into a String
and then back into a byte[]
again.
I tried to just convert the byte[]
to String
and then back to byte[]
again (to make sure thats its not my operations that are causing problems).
So it's like:
// where reply is a byte[]
String str= new String(reply,0, bytesRead);
streamToClient.write(str.getBytes(), 0, bytesRead);
is not equivalent to
streamToClient.write(reply, 0, bytesRead);
my proxy works fine when I just send the byte[]
without any conversion but when I convert it from byte[]
to a String
and then back to a byte[]
its causes problems.
Can some one please help? =]
There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.
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. The simplest way to do so is using valueOf() method of String class in java.
Byte objects are sequence of Bytes, whereas Strings are sequence of characters. Byte objects are in machine readable form internally, Strings are only in human readable form. Since Byte objects are machine readable, they can be directly stored on the disk.
A byte in Go is an unsigned 8-bit integer. It has type uint8 . A byte has a limit of 0 – 255 in numerical range. It can represent an ASCII character.
The best way to convert a byte[]
to String
and back into a byte[]
is not to do it at all.
If you have to, you must know the encoding that was used to produce the byte[]
, otherwise the operation uses the platform default encoding, which can corrupt the data because not all encodings can encode all possible strings, and not all possible byte sequences are legal in all encodings. This is what's happening in your case.
As for how to find out the encoding, that depends:
<meta http-equiv>
headerIf there is no way to find out the encoding you have random garbage, not text data.
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