mawia
mawia

Reputation: 9349

Byte to String| Java

I had a requirement where in I had to write some text on an output stream and reading that text back somewhere else using an input stream.

Now to write the output I'm converting it to byte array using string.getByte(). Now while reading, string is formed using the constructor String(byte[]).

Having said that, the question here is that the read string is not what I would have liked to see. While all the alphanumeric character is getting read as they were, but for other special character in the written string their ASCII value is getting printed preceded by % symbol.

Even the most mundane thing like converting the string to byte array and then reconstructing the string, something like this

    private stringToByte(){
      String data="\"X=Y;Z=A;B=C;\""
      byte[] byteArray=data.getByte();
      String readData=new String(byteArray);
      System.out.println("data:"+readData);
     }

is producing the same result. Now the string which is getting generated after this looks like

   data:%22X=Y%3BZ=A%3BB=C%3B%22

Now my initial assumption was that encoding is being done using default character set, so should it be encoding. But this behavior is apparent even on same machine in same JVM.

I tried going hit and trail way by using

    String readData=new String(byteArray,"diffent character set name");

But, as it should have, it aggravated the situation as I was randomly trying different character set name.

Hopefully you will brake my jinx with it.Can you point where I'm going stupid in this scheme?

Many many thanks in advance! :)

Upvotes: 1

Views: 482

Answers (3)

Durandal
Durandal

Reputation: 20069

Your example code (ignoring typos) works fine when I test it on my machine. Also, the notion of %XX for special characters suggests that there is a lot more going on than you describe, this looks more like Percent Encoding. And that doesnt look like its caused by String.getBytes() at all.

More likely the server you talk to uses percent encoding and you fail to decode it.

Upvotes: 1

pcalcao
pcalcao

Reputation: 15990

Your example works on my machine (correcting some typos there, like a missing ; and getBytes instead of getByte). Did you try to force the encoding with something like:

byte[] byteArray=data.getBytes(Charset.forName("UTF-8"));
String readData=new String(byteArray, Charset.forName("UTF-8"));    

?

Upvotes: 3

Adam
Adam

Reputation: 36743

Use Charset.forName(...) like this. You can also query the available charsets, example below.

String data = "\"X=Y;Z=A;B=C;\"";
byte[] byteArray = data.getBytes(Charset.forName("ISO-8859-1"));
String readData = new String(byteArray, Charset.forName("ISO-8859-1"));
System.out.println("data:" + readData);

for (String name : Charset.availableCharsets().keySet()) {
    System.out.println(name);
}

Upvotes: 4

Related Questions