29

I use below code to convert byte to string:

System.out.println("string " + Byte.toString((byte)0x63));

Why it print "string 99". How to modify to let it print "string c"?

brian
  • 6,802
  • 29
  • 83
  • 124
  • It is impossible to *just* convert a byte to a string. [You must use a character encoding](http://stackoverflow.com/questions/10611455/what-is-character-encoding). – Raedwald Apr 10 '15 at 12:18

10 Answers10

46
System.out.println(new String(new byte[]{ (byte)0x63 }, "US-ASCII"));

Note especially that converting bytes to Strings always involves an encoding. If you do not specify it, you'll be using the platform default encoding, which means the code can break when running in different environments.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 14
    To avoid the checked `UnsupportedEncodingException` thrown by that constructor you'd probably want to pass in [`StandardCharsets.US_ASCII`](http://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html#US_ASCII) instead of using the charset name. – gustafc Dec 28 '11 at 10:20
23

The string ctor is suitable for this conversion:

System.out.println("string " + new String(new byte[] {0x63}));
Matten
  • 17,365
  • 2
  • 42
  • 64
  • 3
    Because gustafc's answer has a very important point: String(byte[]) constructor uses the System default encoding to convert the byte array into String characters. One should not assume that a 0x63 byte value is mapped to the letter 'c'. For example, in UTF-16 the letter 'c' is represented by 2 encoding bytes, not one. This blog post elaborates on this: http://www.joelonsoftware.com/articles/Unicode.html – tbacker Jul 03 '14 at 23:01
15

Use char instead of byte:

System.out.println("string " + (char)0x63);

Or if you want to be a Unicode puritan, you use codepoints:

System.out.println("string " + new String(new int[]{ 0x63 }, 0, 1));

And if you like the old skool US-ASCII "every byte is a character" idea:

System.out.println("string " + new String(new byte[]{ (byte)0x63 },
                                          StandardCharsets.US_ASCII));

Avoid using the String(byte[]) constructor recommended in other answers; it relies on the default charset. Circumstances could arise where 0x63 actually isn't the character c.

gustafc
  • 28,465
  • 7
  • 73
  • 99
6

You can use printf:

System.out.printf("string %c\n", 0x63);

You can as well create a String with such formatting, using String#format:

String s = String.format("string %c", 0x63);
MByD
  • 135,866
  • 28
  • 264
  • 277
3

you can use

the character equivalent to 0x63 is 'c' but byte equivalent to it is 99

System.out.println("byte "+(char)0x63); 
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
2

You have to construct a new string out of a byte array. The first element in your byteArray should be 0x63. If you want to add any more letters, make the byteArray longer and add them to the next indices.

byte[] byteArray = new byte[1];
byteArray[0] = 0x63;

try {
    System.out.println("string " + new String(byteArray, "US-ASCII"));
} catch (UnsupportedEncodingException e) {
    // TODO: Handle exception.
    e.printStackTrace();
}

Note that specifying the encoding will eventually throw an UnsupportedEncodingException and you must handle that accordingly.

dimme
  • 4,393
  • 4
  • 31
  • 51
1

This is my version:

public String convertBytestoString(InputStream inputStream)
{
    int bytes;
    byte[] buffer = new byte[1024];

    bytes = inputStream.read(buffer);
    String stringData = new String(buffer,0,bytes);

    return stringData;

}
Kundan
  • 99
  • 2
  • 4
1

If it's a single byte, just cast the byte to a char and it should work out to be fine i.e. give a char entity corresponding to the codepoint value of the given byte. If not, use the String constructor as mentioned elsewhere.

char ch = (char)0x63;
System.out.println(ch);
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
1
String str = "0x63";
int temp = Integer.parseInt(str.substring(2, 4), 16);
char c = (char)temp;
System.out.print(c);
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0

Using StringBuilder class in Java:

StringBuilder str = new StringBuilder();
        for (byte aByte : bytesArray) {
            if (aByte != 0) {
                str.append((char) aByte);
            } else {
                break;
            }
Elmar
  • 2,235
  • 24
  • 22