I would like to print
some 0xff
bytes using Python 3.5.2 to pipe them into another program. I have a simple C program which reads from stdin
and prints each char in hex:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main () {
char *buffer = malloc(0x100);
fgets(buffer, 0x100, stdin);
for (int i = 0; buffer[i] != 0; printf("%x ", buffer[i++]));
}
If I echo 0xff
byte and pipe it into my program, chars are displayed as expected (a
is because of newline):
# echo -e "\xff\xff\xff\xff" | ./print-chars
ffffffff ffffffff ffffffff ffffffff a
I can also do it with Perl:
# perl -e 'print "\xff\xff\xff\xff"' | ./print-chars
ffffffff ffffffff ffffffff ffffffff
But when I try it in python, the result is different:
# python -c 'print("\xff\xff\xff\xff")' | ./print-chars
ffffffc3 ffffffbf ffffffc3 ffffffbf ffffffc3 ffffffbf ffffffc3 fffffbf a
My questions:
- Why does Python print
0xc3
and0xb3
bytes instead of0xff
? - What is the proper way of printing
0xff
using Python (I want to achieve the same result as with echo or Perl)?