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?

2
  • What do you think happens when you cast a char containing a negative value to an unsigned char? Commented Jun 26, 2019 at 9:01
  • 1
    For positive values it won't change. See stackoverflow.com/questions/1049722/what-is-2s-complement Commented Jun 26, 2019 at 9:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.