简体   繁体   English

获取c中输入字符的ASCII值

[英]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 : 您的代码中有一些错误:

  1. You have used "" a few times instead of ''. 您已多次使用“”而不是“”。 Correct this.For special situations use the escape sequence ie, for writing the \\ character use '\\\\' or for ' character use '\\''. 更正此错误。在特殊情况下,请使用转义序列,即,写\\字符时应使用'\\\\'或'字符时应使用'\\'。
  2. The total number of elements are 95 not 94. 元素总数为95而不是94。
  3. Remove &xa from printf and use xa. 从printf中删除&xa并使用xa。
  4. No Need of adding 32 to xa. 无需在xa中添加32。

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.

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