I have 2 classes, the first is fed a binary string of 1s and 0s to standard in, and splits the string into sub strings of length 8 to convert to bytes, to write to standard out like so:
byte b = (byte)Integer.parseInt(byteString, 2);
System.out.write(b);
System.out.flush();
The second is piped the output of the first, and ultimately will turn the bytes back into the long binary string, but currently I have it just doing this:
int next;
while((next = System.in.read())!=-1){
System.out.print(next); System.out.print(" ");
System.out.flush();
}
If I replace System.out.write(b);
with System.out.print(b); System.out.print(" ");
in the first class with some small sample input, the output is -80 -84 83 11 98 -116 53 -119 49 27 10 -80 -72 104 -123 0
If I pipe this output to the second class it outputs 63 63 83 11 98 63 53 63 49 27 13 10 63 63 104 63 0 13 10
Two things seem to be happening that I can't figure out why, System.in.read()
is returning 63
, which is ASCII for ?
when reading bytes that would be signed negative, and a carriage return + new line has been added to the end. I am stumped for solutions, insight is greatly appreciated.
Note: I was running this in Windows 10 powershell, but on normal command line and in a linux terminal the behaviour was as expected, so problem is only in powershell!