How to convert Bytes Array to a String in order to see it printed onto the console?

风格不统一 提交于 2020-01-25 06:57:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!