obtaining ASCII values of input chars in c -


i intending create simple function take string input , output equivalent of 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 = 0;             for(i = 0; < 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 1 byte number. when represents ascii character number it. example when char x = 'a', saying char x = 65. 1 byte in memory representing x stores number 65. if did x+1 66 or 'b' depending on how print it. when tell print char ascii table , print character. if tell print decimal print 65. example:

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

this print 65. not need conversion table ascii values.

no need ascii arrays , loop inside code.

this

for(i = 0; < 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 simplified to

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

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -