0

I have a question, being a student I have to use OpenSSL. My task is to calculate a RSA signature with blinding, and it uses both char and unsigned char. I said this in case anyone is wondering why I'm using these types, char and unsigned char, so intricately.

My question is what are the rules and good practice advice to using a cast from and to char / unsigned char applied to itself without a pointer, to vector/pointers and, why not, to matrices.

If anymore details are needed I am happy to comply. It goes without saying that I have searched but couldn't find a satisfying answer to anything else than casting int and unsigned int, thus if this is a repost point me in the right direction without hesitation.

1 Answers1

1
char yourChar = 'a';
unsigned char yourUChar = static_cast<unsigned char>(yourChar);

int yourInt = 1;
unsigned int yourUInt = static_cast<unsigned int>(yourInt);

A vector is just multiple ints, you'd cast each member in the vector. Same goes with a matrix.

If you cast a signed value that has a negative value, to an unsigned type, it will likely produce undesired behavior. For example, casting -1 to an unsigned int would give you the bitwise value of -1 cast to an unsigned int. A 32-bit signed int that is -1 is 0xFFFFFFFF in hex, all 1s in binary, and casting that to unsigned would leave the bits as 1s but not you have the largest possible 32-bit unsigned int, which is 4,294,967,295.

Nic Foster
  • 2,864
  • 1
  • 27
  • 45
  • Basically you are saying that one can freely cast from `unsigned` to plain form. The problem occurs when _casting a negative_, say, `char` to an `unsigned char` because of the memory representation. Another side question, are there any real-world negative char representation, because from my knowledge, the ASCII, has representation only for non-negative values? The reason I am asking this is, because, if there is no way one can input from the keyboard negative values, then you can openly cast signed `char` to `unsigned` without worrying for data alteration, right? – Rares Sabin Rusu Jun 16 '15 at 16:01
  • The only safe way to cast from signed to unsigned for numerical values without changing the output of that type is to make sure that the signed value is in the unsigned value's range before casting. So make sure the number isn't less than 0. unsigned chars are pretty rare, I've only ever seen them used when you want to store numbers as characters. If you're using characters for text, just use char. If you want to safely go from char to unsigned char, then (int)char must be positive. I'd read here for more: http://stackoverflow.com/questions/75191/what-is-an-unsigned-char – Nic Foster Jun 16 '15 at 16:39
  • 1
    @RaresSabinRusu: "are there any real-world negative representations" -- Virtually *any* real-world text will have values outside the ASCII-7 range in it. "Josè payed 15€." There are two characters that would be negative for signed chars, assuming Latin-9 encoding (as € does not exist in Latin-1 and UTF-8 would have *five* bytes with negative representation.) Welcome to the wild world of text encoding. – DevSolar Apr 29 '16 at 11:13
  • @RaresSabinRusu: C++ defines a _Basic literal character set_ containing exactly 100 characters, whose values are all guaranteed non-negative (but not necessarily the ASCII values). But there's also the _Execution character set_, which is a superset of the _Basic literal character set_. For these additional characters, the encoding (including sign) is unspecified – MSalters Nov 08 '22 at 13:31