1

I'm writing an algorithm requires me to insert values into the std::string via an unsigned 1 byte integer (0 - 255) and calls for me to print the values of the individual characters of the string as integers, however, I keep getting negative values. My wild guess is that the characters are stored in the std::string as signed 1 byte characters (-128 to 127), hence, why I'm getting negative values for output. The negative values are the equivalent negative forms of the positive values I'm inserting. I did a bit of research, but couldn't seem to find a way to word my question in a way that produced the answer I was looking for.

JosephTLyons
  • 2,075
  • 16
  • 39
  • 3
    I'm not sure what this has to do with `std::string`, its rather the question whether `char` is unsigned or signed. You got your answer already, so, whats the question? – tkausl Sep 15 '18 at 08:47
  • 2
    `std::string` is defined as `std::basic_string`, and whether `char` is signed or not is system-dependent. – Salem Sep 15 '18 at 08:47
  • 1
    Back in the day when I was working with bytes off a socket connection, I'd do a `int value = inbyte & 0xFF;`. The inbyte was a char, and the 0xFF would result in an int in the range of 0 to 255. – Eljay Sep 15 '18 at 12:42

2 Answers2

6

No, this is implementation-dependent.

std::string is an alias for std::basic_string<char>, so the question boils down to the signedness of char on your plarform/implementation.

If you want it unsigned, explicitly convert it:

std::cout << static_cast<int>(static_cast<unsigned char>(ch));

Or alternatively, as suggested in comments, use vector<uint8_t> instead of string.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • Thank you. This is what I'm currently doing. I'm working on an encryption algorithm that requires me to perform some math on the bit values, so I think I will have to cast the values both before I perform the math on the bit values of the characters before storing them back into the string, and before printing. – JosephTLyons Sep 15 '18 at 08:55
  • 4
    @joe_04_04 Use a `std::vector` instead of `std::string`. – πάντα ῥεῖ Sep 15 '18 at 08:57
  • @πάνταῥεῖ, hmmm, interesting suggestion. Thank you, I'll consider this. – JosephTLyons Sep 15 '18 at 09:04
  • @joe_04_04 - Just please, whatever you do, do not use some form of home grown crypto unless this is just some sort of pet project or academic exercise. Use one of the standard algorithms, and better still would be to use one of the standard crypto libraries like [Crypto++](https://www.cryptopp.com/). A library that is open and has been peer reviewed and tested. Crypto is [hard](https://www.schneier.com/blog/archives/2016/03/cryptography_is.html). – pstrjds Sep 15 '18 at 09:51
  • This is for a course in security: an academic endeavor. – JosephTLyons Sep 15 '18 at 09:52
3

This has nothing really to do with string, it's just that char may be a signed type. All you need to do is cast your char to unsigned char. E.g.

char some_char = ...;
cout << (int)(unsigned char)some_char;

or

string some_string = ...;
cout << (int)(unsigned char)some_string[0];
john
  • 85,011
  • 4
  • 57
  • 81