将非常大的字符串转换为整数格式

时间:2021-01-23 03:11:17

标签: c string

我在输入中有一个 200 个字符的字符串(所有字符都是 0 或 1)。需要一种方法将字符转换为 int 格式并将它们存储在数组或变量中。

示例:"00101110010..." -> 00101110010...

下面的代码片段仅适用于 <=20 个字符的字符串。

    scanf("%u", &digit_num);   //number of digits in input number/string
    unsigned input_binary[digit_num]; //where the integers will be stored as individual digit
    unsigned long long temp; //temporary storage
    char crc[digit_num+1], *ptr=NULL;
    scanf(" %s", crc);
    temp=strtoull(crc,&ptr,10);
    for (unsigned j = 0; j < digit_num; j++)
    {
        input_binary[digit_num-j-1]=temp%10;
        temp/=10;
    } 

1 个答案:

答案 0 :(得分:1)

for (unsigned j = 0; j < digit_num; j++)
{
  input_binary[j]=crc[j]-48;
} 

'0' 的 ASCII 码是 48。 char('3') - 48 = int(3)

相关问题