How to print raw byte content from a byte[] array to stdout in Java?

前端 未结 2 1176
梦毁少年i
梦毁少年i 2020-12-09 17:52

I am doing the same project as describe here:

Wrap deflated data in gzip format

My problem is that when I try to print out bytes, I get weird results. My pro

相关标签:
2条回答
  • Assuming your byte array is called buf:

     System.out.println(Arrays.toString(buf));
    

    Edit: It sounds like what you really want to do is write your bytes to stdout, not print them. See http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html for the difference between printing to a stream and writing to it. Easiest way should be to call the write(byte[] b) method:

    System.out.write(buf);
    
    0 讨论(0)
  • 2020-12-09 18:48
     /* There is an image / ic_launcher in the drawable folder for which I am making ByteArray   */
    
    
    
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.img);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] mByteArray = stream.toByteArray();
    

    /* here I am showing the raw data not in hexadecimal format */

        System.out.println(Arrays.toString(mByteArray));
    

    I think this will help you guys!

    0 讨论(0)
提交回复
热议问题