System.out.print of string without 'ln'

后端 未结 4 1620
长情又很酷
长情又很酷 2021-01-20 00:39

I have a byte array which looks like this:

[0, 0, 0, 0, 0, 0, 0, 0, 122, 98, 117, 54, 46, 0, 0, 115, 122, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 121, 116,          


        
4条回答
  •  盖世英雄少女心
    2021-01-20 01:04

    Your byte array is mostly non printable characters, with a few random letters mixed in. But all you need is:

    String myString = new String(byteArray);
    

    which will give you a valid string.

    Try out the following code, as maybe it will better illustrate the issue your having:

    for (char c : new String(byteArray).toCharArray()) {
        System.out.printf("Character: %s Hex: %02x \n", c, (int) c);
    }
    

提交回复
热议问题