简体   繁体   English

C ++ int转换为char

[英]c++ int to char

I have a list of ints i ([0,255]) and I want to convert these ints to chars. 我有一个整数i([0,255])的列表,我想将这些整数转换为chars。

int i;
char c = (char)i;

When I print c (when i=0) I do not get the correponding ascii symbol. 当我打印c时(当i = 0时),我没有得到相应的ascii符号。 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. 您正在将ASCII值与int值混合在一起...查看ASCII表,您将了解。 To print a '0' you need to print the value 48 (decimal). 要打印“ 0”,您需要打印值48(十进制)。 To print values from 0 to 9 you can simply add 48 to the int value and you'll get the correct ASCII code. 要打印从0到9的值,您只需在int值上加上48,就可以得到正确的ASCII码。 To print values longer then one digit you need some kind of conversion from one int to several ASCII codes. 若要将值打印的时间长于一位,则需要从一种int到几种ASCII代码的某种转换。 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. 我认为值0不会包含有意义的ASCII符号。它是NULL字符,不应输出任何内容。 Why not try printing a value when i = 97 ('a'), for instance? 例如,为什么不尝试在i = 97 ('a')时打印值?

If you examine an ASCII table , you'll realize that not every ASCII character has a meaningful printed value. 如果检查ASCII表 ,您将意识到并非每个ASCII字符都有有意义的打印值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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