0

I saw this question : How to convert an ASCII char to its ASCII int value?

The most voted answer (https://stackoverflow.com/a/15999291/14911094) states the solution as :

Just do this:

int(k) 

But i am having issues with this.

My code is :

std::cout << char(144) << std::endl; std::cout << (int)(char(144)) << std::endl; std::cout << int('É') << std::endl; 

Now the output comes as :

É -112 -55 

Now i can understand the first line but what is happening for the second an the third lines?

Firstly how can some ASCII be negative and secondly how can that be different for the same character.

Also as far as i have tested this is not some random garbage from the memory as this stays same for every time i run the program also :

If i change it to 145 :

æ -111 

The output to changes by 1 so as far as i guess this may due to some kind of overflow.

But i cannot get it exactly as i am converting to int and that should be enough(4 bytes) to store the result.

Can any one suggest a solution?

7
  • I think using unsigned char will work, it has a maximum of 255 Commented May 14, 2021 at 10:42
  • First of all AFAIK ASCII is defined only from 32 to 127. Anything else is not ASCII. In your case char type is one byte signed type. I.e. it can keep values -127..128. So, when you do char(144) it is stored as -112 and when you do (int) it just converts single byte int to C int without sign conversion. PS: Value of int('É') can differ from 144 because your source code is encoded with different encoding from terminal, probably UTF-8. Commented May 14, 2021 at 10:43
  • @debanshudas: That's the minimum maximum value allowed by the C and C++ standards. Commented May 14, 2021 at 10:44
  • 1
    @sklott: Characters 0 through to 31 are defined in ASCII too. Commented May 14, 2021 at 10:47
  • 1
    @sklott • ASCII 127 is not a displayed character. Commented May 14, 2021 at 11:08

2 Answers 2

1

If your platform is using ASCII for the character encoding (most do these days), then bear in mind that ASCII is only a 7 bit encoding.

It so happens that char is a signed type on your platform. (The signedness or otherwise of char doesn't matter for ASCII as only the first 7 bits are required.)

Hence char(144) gives you a char with a value of -112. (You have a 2's complement char type on your platform: from C++14 you can assume that, but you can't in C).

The third line implies that that character (which is not in the ASCII set) has a value of -55.

int(unsigned char('É')) 

would force it to a positive value on all but the most exotic of platforms.

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

Comments

0

The C++ standard only guarantees that characters in the basic execution character set1 have non-negative encodings. Characters outside that basic set may have negative encodings - it depends on the locale.


  1. Upper- and lowercase Latin alphabet, decimal digits, most punctuation, and control characters like tab, newline, form feed, etc.

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.