Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a single byte to a string?

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.

like image 352
JuiCe Avatar asked Jul 27 '12 13:07

JuiCe


1 Answers

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

new String(new byte[] { recBuf[0] })
like image 54
SLaks Avatar answered Nov 03 '22 11:11

SLaks