Take the 2-minute tour ×

This is simply to error check my code, but I would like to convert a single byte out of a byte array to a string. Does anyone know how to do this? This is what I have so far:

recBuf = read( 5 );
Log.i( TAG,  (String)recBuf[0] );

But of course this doesn't work.

I have googled around a bit but have only found ways to convert an entire byte[] array to a string...

new String( recBuf );

I know I could just do that, and then sift through the string, but it would make my task easier if I knew how to operate this way.

share|improve this question
add comment

9 Answers

up vote 4 down vote accepted

You can make a new byte array with a single byte:

new String(new byte[] { recBuf[0] })
share|improve this answer
add comment

There's a String constructor of the form String(byte[] bytes, int offset, int length). You can always use that for your conversion.

So, for example:

    byte[] bite = new byte[]{65,67,68};

    for(int index = 0; index < bite.length; index++)
        System.out.println(new String(bite, index,1));
share|improve this answer
add comment

Use toString method of Byte

String s=Byte.toString(recBuf[0] );

Try above , it works.


 byte b=14;
String s=Byte.toString(b );
System.out.println("String value="+  s);


String value=14
share|improve this answer
add comment

What about converting it to char? or simply

  new String(buffer[0])
share|improve this answer
If you look below, that does not work. –  JuiCe Jul 27 '12 at 13:56
add comment

public static String toString (byte value)

Since: API Level 1 Returns a string containing a concise, human-readable description of the specified byte value.

Parameters value the byte to convert to a string. Returns a printable representation of value.]1 this is how you can convert single byte to string try code as per your requirement

share|improve this answer
developer.android.com/reference/java/lang/… try this link –  Sourabh Saldi Jul 27 '12 at 13:58
add comment

Edit: Hows about

""+ recBuf[0];//Hacky... not sure if would work

Pretty sure that would work.

share|improve this answer
It doesn't, thanks though. –  JuiCe Jul 27 '12 at 13:54
Hmm, I thought it would call .toString on it... although it is a primitive... –  meiamsome Jul 27 '12 at 13:57
add comment

Another alternate could be converting byte to char and finally string

Log.i(TAG, Character.toString((char) recBuf[0]));


Log.i(TAG, String.valueOf((char) recBuf[0]));
share|improve this answer
add comment

You're assuming that you're using 8bit character encoding (like ASCII) and this would be wrong for many others.

But with your assumption you might just as well using simple cast to character like

char yourChar = (char) yourByte;

or if really need String:

String string = String.valueOf((char)yourByte);
share|improve this answer
add comment

Or the 'old' way

Byte x = 1;
String dog = x.toString();
share|improve this answer
add comment

Your Answer


