简体   繁体   中英

c++ int to char

I have a list of ints i ([0,255]) and I want to convert these ints to chars.

int i;
char c = (char)i;

When I print c (when i=0) I do not get the correponding ascii symbol. How to do the mapping correctly ?

ASCII 0没有符号。所有小于32的ASCII值都是控制字符。

You are mixing ASCII values with int values... Look at an ASCII table and you'll understand. To print a '0' you need to print the value 48 (decimal). To print values from 0 to 9 you can simply add 48 to the int value and you'll get the correct ASCII code. To print values longer then one digit you need some kind of conversion from one int to several ASCII codes. A google search should find such a function.

I don't believe there is a meaningful ASCII symbol for the value 0. It's a NULL character and shouldn't print anything. Why not try printing a value when i = 97 ('a'), for instance?

If you examine an ASCII table , you'll realize that not every ASCII character has a meaningful printed value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM