2014-12-04 2 views
0

각 바이트 b를 UTF-8을 사용하여 바이트 배열에서 문자열로 변환해야합니다. 전체 바이트 배열을 UTF-8을 사용하여 문자열로 변환 할 수 있습니다. 도움을 받으십시오. 아래는 바이트 배열 인 버퍼가있는 코드입니다. 이것은 예입니다UTF-8을 사용하여 바이트 배열을 바이트 배열에서 문자열로 변환

텍스트 [바이트 형식] : [B의 @의 187aeca

텍스트 [

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       
}  

답변

0

이 Exmaple 당신에게

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); 
    } 
} 

출력

텍스트 도움이 될 수 있습니다 바이트 형식] : [B @ 187aeca

해독 된 텍스트 : 이것은 예입니다

+0

net.But에서 찾았지만 UTF-8을 사용하여 바이트를 디코딩해야하므로 도움이되지 않았습니다. – coder

0

각 바이트를 char으로 간단히 전송하십시오.

for (byte b : buffer) { 
     // Here I need to convert Each Byte b to string using UTF-8 
     System.out.println((char) b); 
    } 
관련 문제