首页 > 解决方案 > How to convert a character array to a decimal integer array?

问题描述

I would like to convert a character array to a decimal array. For example, in the extended ASCII Table, lowercase q is 113. I would ask a prompt to get a text from the user and then the user might input "qqq", then input[] would be ['q','q','q'] and intinput[] would be ['113','113','113'].

Is there a way to do this? Thank you

char input[50], ch;
int intinput[50];
int j = 0, l = 0;

printf("Enter Cleartext: ");
while((ch = getchar()) != '\n') {
    input[j] = ch;  
    j++;
}

for(l = 0;input[l] != '\0'; l++) {
    intinput[l] = input[l] - '0';
}

the "intinput[l] = input[l] - '0';" does not work.

标签: c

解决方案


In the for loop try

for(l =0; input[l]!= '\0'; l++)
{
    intinput[l] = (int)input[l];
}

推荐阅读