C 如何将字符数组转换为十进制整数数组?
我想将C 如何将字符数组转换为十进制整数数组?,c,C,我想将字符数组转换为十进制数组。例如,在扩展ASCII表中,小写的q是113。我会要求提示从用户那里获取文本,然后用户可能会输入“qqq”,然后输入[]将是['q','q','q'],而输入[]将是['113','113','113'] 有办法做到这一点吗?多谢各位 char input[50], ch; int intinput[50]; int j = 0, l = 0; printf("Enter Cleartext: "); while((ch = getchar()) != '\n'
字符数组
转换为十进制数组
。例如,在扩展ASCII表中,小写的q是113。我会要求提示从用户那里获取文本,然后用户可能会输入“qqq”,然后输入[]将是['q','q','q'],而输入[]将是['113','113','113']
有办法做到这一点吗?多谢各位
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';
}
“intinput[l]=input[l]-“0”;“
不起作用。在for循环中尝试
for(l =0; input[l]!= '\0'; l++)
{
intinput[l] = (int)input[l];
}
在for-try循环中
for(l =0; input[l]!= '\0'; l++)
{
intinput[l] = (int)input[l];
}
如果你想做我认为你想做的事情,这会让你在大部分的道路上达到目的。您的示例似乎并不表示您希望创建像这样的整数 ['0','0','0']到[0,0,0],所以我没有做任何减法。您的示例指示直接ascii转换 以下是使用您提供的代码给出的答案[113113113,0,0,0,0..0]:
char input[50] = { 0 }, ch;
int intinput[50] = { '\0' };
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] = (int)input[l];
}
如果你想做我认为你想做的事情,这会让你在大部分的道路上达到目的。您的示例似乎并不表示您希望创建像这样的整数 ['0','0','0']到[0,0,0],所以我没有做任何减法。您的示例指示直接ascii转换 以下是使用您提供的代码给出的答案[113113113,0,0,0,0..0]:
char input[50] = { 0 }, ch;
int intinput[50] = { '\0' };
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] = (int)input[l];
}
在变量中存储ASCII字符时,它们将作为ASCII值存储。所以你不需要做映射。反过来也是一样的。也就是说,您可以将int转换为char。然后,由于这两种类型的大小不同,前8个字节将被复制到char,当您打印时,它将显示响应该字节值的字符 在变量中存储ASCII字符时,它们将作为ASCII值存储。所以你不需要做映射。反过来也是一样的。也就是说,您可以将int转换为char。然后,由于这两种类型的大小不同,前8个字节将被复制到char,当您打印时,它将显示响应该字节值的字符 您不需要为此进行类型转换,只需分配即可:
#include <stdio.h>
int main(void)
{
char char_arr[50] = {[0 ... 49] = '\0'};
int int_arr[50] = {[0 ... 49] = 0};
int count=0;
int input;
int loop_var=0;
while((input=getchar())!=EOF)
{
if(input == '\n')
break;
char_arr[count] = input;
int_arr[count] = input;
++count;
}
while(loop_var<count)
{
printf("Character: %c Corresponding Integer: %d\n",char_arr[loop_var],int_arr[loop_var] );
loop_var++;
}
return 0;
}
您不需要为此进行类型转换,只需分配即可:
#include <stdio.h>
int main(void)
{
char char_arr[50] = {[0 ... 49] = '\0'};
int int_arr[50] = {[0 ... 49] = 0};
int count=0;
int input;
int loop_var=0;
while((input=getchar())!=EOF)
{
if(input == '\n')
break;
char_arr[count] = input;
int_arr[count] = input;
++count;
}
while(loop_var<count)
{
printf("Character: %c Corresponding Integer: %d\n",char_arr[loop_var],int_arr[loop_var] );
loop_var++;
}
return 0;
}
我测试了这个,它确实有效,尽管可能有更好的答案我测试了这个,它确实有效,尽管可能有更好的答案