Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force std::string to use unsigned char (0 to 255) instead of char (-128 to 127)

Tags:

c++

string

Is there a way to force string use unsigned char instead of char? Perhaps in constructor?

I need to do arithmetic operations (mostly incrementing, decrementing, ~ (bitwise NOT)), and I need to be able to use overflow 255++ == 0... Not 127++ == -128 (and underflow 0-- == 255...)

I am not sure if question makes sense, to put some light on it here is a good question abou topic (streams) Why do C++ streams use char instead of unsigned char?

I am not trying to convert string to unsigned char I found a lot of questions how to convert between the two.

like image 908
Kyslik Avatar asked Jan 29 '26 05:01

Kyslik


1 Answers

You can use:

typedef std::basic_string<unsigned char> ustring;

And then use ustring instead of std::string

like image 92
Mohit Jain Avatar answered Jan 30 '26 21:01

Mohit Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!