C++中怎样把数字转换成对应的ASCII值?

前面需要加什么函数库吗?还有我问一道题“键入5个英文国家名,按字典规律排序后输出”,把程序给我,对的话追加10分!

//字典顺序是按ASCII码值排序的,若想把数字转换为ASCII码,只须+上'0'就可以了,你可以查一下ASCII码字符集(c++教程的附录通常都有这表),就可以很清楚地知道大写字母、小写字母、数字之间转换关系!#include <iostream>
#include <string>
using namespace std;int main()
{
char* str[5]; //注:字典顺序是按ASCII码排序的,小写的ASCII码值大于所有大写的ASCII码!
cout<<"请输入五个国家名字(英文):"<<endl;
int i,j,t;
for(i=0;i<5;i++)
{
str[i]=new char[20];
cin>>str[i];
}
for(i=0;i<4;i++)
{
t=i;
for(j=i+1;j<5;j++)
{
if(strcmp(str[t],str[j])>0)
{
t=j;
}
}
if(t!=i)
{
char temp[20];
strcpy(temp,str[t]);
strcpy(str[t],str[i]);
strcpy(str[i],temp); //交换
}
} for(i=0;i<5;i++)
{
cout<<str[i]<<'\t';
}
cout<<endl;
return 0;
}输出结果如下:(1)首字母为大写,排序如下:(2)首字母为小写的,排序如下:(3)首字母不统一的情况,则排序情况有点不一样,不要光看表面的字母,c++的小写字母ASCII码比大写字母的大!,排序如下:
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-24
#include <stdio.h>
#include <string>
void fun1()
{
int a[]={1,2,3,4,5,6,7,8,9,0,1};
char b[11];
for(int i=0;i<11;i++)//将整形转换成字符型。
{
b[i]=a[i]+48;
}
b[i]='\0';
puts(b);

}
void fun2()
{
char b[]="123456789101112";//共15个字符
int  a[15];
int len;
len=strlen(b);
puts(b);

for(int i=0;i<len;i++)//将字符型转换成整型
{
a[i]=b[i]-48;
}

for(i=0;i<len;i++)
{
printf("%d",a[i]);
}

}
int main()
{
fun1();//将整形转换成字符型。
fun2();//将字符型转换成整型。
return 0;
}

不是太明白你的意思所以实现了两种互相转换。

其实,在计算机的存储中,都是存储数字。

也就是说,你要什么,只要控制输出的格式就够了。

int a[]={1,2,3,4,5,6,7,8,9,0,1};

for(int i=0;i<15;i++)

{

printf("%c",a[i]+48);

}

关键是数字。

字符是数字的特殊表现形式。

第2个回答  2013-05-10
加'0'就行了比如int a=3;char b = a+'0';
相似回答