0

I asked for help with this problem in which, when I want to convert bytes from table

`[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, 117, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 107, 111, 98, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122, 42, 109, 111, 119, 40, 0, 0, 0, 0, 0, 0, 107, 111, 98, 0, 0, 0, 0, 0, 98, 111, 40]`

into characters and print this characters. When I make:

    for(byte b: byteArray){
        System.out.print((char) b);
    }

I don't have anything on my console. Even the scroll bars are the same length. But when I make:

for(byte b: byteArray){
 System.out.println((char) b);
}

it works good. I think this is eclipse problem, not with the code. I unchecked the limit console output, but it didn't helped.

Community
  • 1
  • 1
Rop
  • 217
  • 2
  • 12
  • 2
    You are aware that certain encodings are not one-byte-per-char, right? What encoding do you use, and why don't you use the `String(byte[], Charset)` constructor? – fge Mar 19 '14 at 08:39
  • How about `System.out.print(new String(byteArray));` – Adil Shaikh Mar 19 '14 at 08:41
  • @MohammadAdil I was trying also that. But still nothing appears – Rop Mar 19 '14 at 08:42
  • @Rop i tried System.out.print((char) b); and in my eclipse console it works fine. There must be something wrong with ur eclipse. And Mohammeds solution worked too. – kai Mar 19 '14 at 08:46
  • @kai I guessed, but I don't know what my be wrong with my eclipse – Rop Mar 19 '14 at 08:48

5 Answers5

0

I'd try to exclude the non-printable chars, like 0

for(byte b: byteArray){
    System.out.print(b >= 32 ? (char) b : "");
}

You can replace the blank String for whatever you need as "?" or "[ascii_code]"...

I've tested using your data and In my Eclipse (OSX) appears in both cases, my code:

public static void main(String[] args) {
    byte[] bytes =
        { 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, 117, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 107, 111, 98, 105, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 122, 42, 109, 111, 119, 40, 0, 0, 0, 0, 0, 0, 107, 111, 98, 0, 0, 0, 0, 0, 98, 111, 40 };
    System.out.println("Init...");
    for (byte b : bytes) {
        System.out.print((char) b);
    }
    System.out.println();
    System.out.println();
    for (byte b : bytes) {
        System.out.print(b >= 32 ? (char) b : "?");
    }
    System.out.println();
    System.out.println("End");
}

The output for that is:

Init...
zbu6.sz*tytul kobiz*mow(kobbo(

????????zbu6.??sz*???????????tytul?????????????????????? kobi????????????????????????????????????????????????????????????????????????????????????z*mow(??????kob?????bo(
End
Roberto
  • 8,586
  • 3
  • 42
  • 53
0

char is of 2 byte , if byte number is out of range -128 to 127 ,

Vivek Keshri
  • 108
  • 10
0

try to do

for(byte b: byteArray){
    System.out.print((char) b);
}
System.out.println();

The method print may be buffered and not shown while you don't print \n

Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
0

the problem is in your environment for sure.

my eclipse console output this with your first loop:

enter image description here

funny thing, i can't copy/paste this output. i think it's \0 bytes fault.

Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73
0

The console is reacting badly to the null character \u0000 which is often used as a string terminator.

Eclipse uses SWT which often uses native controls where possible so it may be that there's a defect in the underlying UI toolkit or how SWT uses it.

This has been reported before - Bug 362957 - The Console does not display '\u0000' correctly - so you might want to add your input to the bug report and vote on it.

I would include:

  • Operating system/version
  • Eclipse and SWT build versions (look in the plugins directory)
    • any changes if any you've made to the stylesheets (e.g. e4_default_gtk.css)
  • The platform UI toolkit version if applicable (e.g. GTK)
  • The font - see Preferences
  • Java version (probably irrelevant)
McDowell
  • 107,573
  • 31
  • 204
  • 267