问题
I am trying to convert a Byte Array to a String in order to be able to see this Vector/Array and analyse how these bytes are organized.
I'm using the code below to do it:
byte[] bytes = bos.toByteArray();
String msgDecode = new String(bytes); // trying to convert byte in String
System.out.println("Vetor de bytes [" + msgDecode + "]"); // Showing it
But it's not working. Why the code above it's no working?
It is worth mentioning that the Byte Array is being constructed according to this other code here:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+UUID.randomUUID().toString()+"audio_record.3gp";
public byte[] convert(String path) throws IOException {
FileInputStream fis = new FileInputStream(path);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
for (int readNum; (readNum = fis.read(b)) != -1; ) {
bos.write(b, 0, readNum);
}
byte[] bytes = bos.toByteArray();
return bytes;
}
The general idea is to record an audio with the smartphone and convert this audio file to a array of bytes. This String path is the path where the audio is being saved after recording. Then I use this path (that represents the audio file) and convert it to a byte array according to the code above.
More information you can find and help me here: How to solve this error: Android resource linking failed?
回答1:
if you want to see value of bytes you can use as below:
Log.d("TAG", Arrays.toString(bytes));
回答2:
What I'll try to do, based in what you guys said:
String decode = Arrays.toString(bytes);
Log.d("mytag", decode);
回答3:
byte[] bytes = "ABCDEFG".getBytes();
System.out.println(new String(bytes)); // ABCDEFG
System.out.println(Arrays.toString(bytes)); // [65, 66, 67, 68, 69, 70, 71]
来源:https://stackoverflow.com/questions/59752616/how-to-convert-bytes-array-to-a-string-in-order-to-see-it-printed-onto-the-conso