I need to convert Each byte b from byte array to string using UTF-8 .I am able to convert entire byte array to string using UTF-8.Please help.Below is my code that have buffer which is byte array.
String str = new String(buffer, "UTF-8");
// convert the array of bytes to characters using the default encoding.
Log.d("es.pymasde.blueterm",str);
// for each byte in the buffer(byte array)
for(byte b:buffer)
{
//Here I need to convert Each Byte b to string using UTF-8
}
This Exmaple may help you
public class TestByte
{
public static void main(String[] argv) {
String example = "This is an example";
byte[] bytes = example.getBytes();
System.out.println("Text : " + example);
System.out.println("Text [Byte Format] : " + bytes);
System.out.println("Text [Byte Format] : " + bytes.toString());
String s = new String(bytes);
System.out.println("Text Decryted : " + s);
}
}
Output
Text : This is an example
Text [Byte Format] : [B@187aeca
Text [Byte Format] : [B@187aeca
Text Decryted : This is an example
Simply cast each byte to char
.
for (byte b : buffer) {
// Here I need to convert Each Byte b to string using UTF-8
System.out.println((char) b);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.