I found solution that I'm happy with. It's more elegant than explicitly casting every constant to unsigned char
. This is what I had:
ss.put( 0xa4 ); // C4309
I thought that the "truncation" was happening in implicitly casting unsigned char
to char
, but Cong Xu pointed out that integer constants are assumed to be signed, and any one greater than 0x7f gets promoted from char
to int
. Then it has to actually be truncated (cut down to one byte) if passed to put()
. By using the suffix "u", I can specify an unsigned integer constant, and if it's no greater than 0xff, it will be an unsigned char
. This is what I have now, without compiler warnings:
ss.put( 0xa4u );