简体   繁体   中英

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. 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. When it represents an ascii character it is actually just the number for it. For example when you say char x = 'A' , you are essentially saying char x = 65 . The one byte in memory representing x really stores the number 65 . If you did x+1 you would get 66 or 'B' depending on how you print it. When you tell it to print a char it will look up the ascii table and print the character. If you tell it to print a decimal it will print 65 . For example:

char x = 'A';
printf("%d", x);

This will print 65 . You do not need a conversion table to look up the ascii values.

No need for ascii arrays and another loop inside your code.

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.
  3. Remove &xa from printf and use xa.
  4. No Need of adding 32 to xa.

The corrected code is:

#include<stdio.h>
#include<process.h>
#include<string.h>
void cls(){
system("cls");
}
void Ascii(){
cls();

// 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");
     }

 }

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