0

There are many questions related to this but they answer conversion of char* to unsigned char*. What I want to ask is conversion of char to unsigned char :

If I cast ( C-style or static-cast ) a char to an unsigned char like :

char c = 'A' ; unsigned char uc = ( unsigned char ) c ;

Can the integral value of c change during the cast ? As char is signed or unsigned, is implementation defined, so wherever it is signed , is it possible that the cast would change the integral value of the character ?

My Primary concern is for positive values. If I cast the 'above' way, is there a possibility of the values being changed?

Ayush
  • 302
  • 2
  • 10
  • What do you think happens when you cast a char containing a negative value to an unsigned char? – gan_ Jun 26 '19 at 09:01
  • 1
    For positive values it won't change. See https://stackoverflow.com/questions/1049722/what-is-2s-complement – VLL Jun 26 '19 at 09:03

1 Answers1

3

Since C++14, char if signed must be 2's complement.

Therefore the cast from signed char to unsigned char and vice-versa cannot change the underlying bit pattern.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483