0x01
is an integer literal. Integer literals are always of integer type with rank at least that of int
. Therefore they cannot be of type char
(or signed char
/unsigned char
).
In your case 0x01
will have type int
.
char
and its signed
/unsigned
variants are the only integral type for which operator<<
on an std::ostream
will output a single character represented by the value held in the char
.
For other integral types operator<<
will output the integer value formatted in decimal representation.
If you want to print the single character from its ASCII code, you need to cast 0x01
to char
:
some_file << char{0x01};
or use a character literal:
some_file << '\x01';
or you can use the put
method to place a single character, which takes a char
parameter and will therefore cause implicit conversion of the int
literal to char
, which will then be printed as one character represented by the value:
some_file.put(0x01);
or
some_file.put('\x01');
Contrary to <<
, put
will write the character directly without change. <<
is affected by formatting options that can be set on the stream, e.g. there could be padding added. If you want to be absolutely sure that only the single character is printed without change, put
is probably the correct choice.