[英]obtaining ASCII values of input chars in c
I was intending to create a simple function that would take a string as its input and output the equivalent of that string in ASCII. 我打算创建一个简单的函数,该函数将字符串作为输入并以ASCII输出该字符串的等效项。 Plz help..
请帮助
void cls(){
system("cls");
}
void getAscii(){
cls();
text(4);
char a[94]={' ','!','"','#','$','%','&',"'",'(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[',"'\'",']','^','_','`','a','b',
'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','{','|','}','~'};
while(1){
char x[5000], *exitMsg = "quit";
gets(x);
if(strcmp(x, exitMsg) == 0){
break;
}else{
int i = 0;
for(i = 0; i < strlen(x); i++){
int j = 0;
for(j = 0; j < 94; j++){
if(x[i] == a[j]){
int xa = (a[j] + 32);
printf("%d", &xa);
}
}
}
printf("\n");
}
}
}
A char
is just an one byte number. char
只是一个字节数。 When it represents an ascii character it is actually just the number for it. 当它代表一个ASCII字符时,实际上只是它的数字。 For example when you say
char x = 'A'
, you are essentially saying char x = 65
. 例如,当您说
char x = 'A'
,您实际上是在说char x = 65
。 The one byte in memory representing x
really stores the number 65
. 内存中代表
x
的一个字节实际上存储了数字65
。 If you did x+1
you would get 66
or 'B'
depending on how you print it. 如果您执行
x+1
,则根据打印方式将得到66
或'B'
。 When you tell it to print a char it will look up the ascii table and print the character. 当您告诉它打印字符时,它将查找ascii表并打印字符。 If you tell it to print a decimal it will print
65
. 如果您告诉它打印一个小数,它将打印
65
。 For example: 例如:
char x = 'A';
printf("%d", x);
This will print 65
. 这将打印
65
。 You do not need a conversion table to look up the ascii values. 您不需要转换表即可查找ascii值。
No need for ascii arrays and another loop inside your code. 不需要ascii数组和代码内的另一个循环。
This 这个
for(i = 0; i < strlen(x); i++){
int j = 0;
for(j = 0; j < 94; j++){
if(x[i] == a[j]){
int xa = (a[j] + 32);
printf("%d", &xa);
}
}
}
can be simplified to 可以简化为
for(i = 0; i < strlen(x); i++) {
printf("%d", x[i]);
}
There are a few errors in your code : 您的代码中有一些错误:
The corrected code is: 正确的代码是:
#include<stdio.h>
#include<process.h>
#include<string.h>
void cls(){
system("cls");
}
void Ascii(){
cls();
// text(4); // text(4); //uncomment this if it does something useful
//取消注释是否有用
char a[95]={' ','!','"','#','$','%','&',' ','(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[','\\',']','^','_','`','a','b',
'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','{','|','}','~'};
while(1){
char x[5000], *exitMsg = "quit";
gets(x);
if(strcmp(x, exitMsg) == 0){
break;
}else{
int i = 0;
for(i = 0; i < strlen(x); i++){
int j = 0;
for(j = 0; j < 94; j++){
if(x[i] == a[j]){
int xa = (a[j] );
printf("%d ", xa);
}
}
}
printf("\n");
}
}
}
ALTHOUGH You require none of this. 尽管您不需要任何。
Try this: 尝试这个:
void cls(){
system("cls");
}
void Ascii(){
cls();
while(1){
char x[5000], *exitMsg = "quit";
gets(x);
if(strcmp(x, exitMsg) == 0){
break;
}else{
int i = 0;
for(i = 0; i < strlen(x); i++){
int xa = (x[i] );
printf("%d ", xa);
}
}
printf("\n");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.