C中的二进制到十进制转换器在一定数量后不起作用
Binary to decimal converter in C doesn't work after a certain number
我试过使用 C 制作正二进制到十进制数的转换器,但是当我尝试输入高于 1110011010110001010111(十进制为 3779671)的值时,程序总是 returns 那个确切的数字。我当前的作业要求它适用于最大为 11111111111111111111111111111 (1073741823) 的二进制数。
到目前为止,我已经尝试将变量类型更改为任何其他可能的更大尺寸,但它似乎不起作用。这是当前代码:
#include <math.h>
void main()
{
unsigned long long int bi, de = 0;
unsigned long long int x = 0, bases;
scanf("%llu", &bi);
for(x=0 ; bi>0 ; x++, bi=bi/10){
bases = bi % 10;
de = de + bases * pow(2,x);
}
printf("%llu", de); // imprime o correspondente em decimal
}
提前感谢您的帮助。
你不能读取二进制数11111111111111111111111111111并放入unsigned long long integer中,因为unsigned long long int的极限是18446744073709551615,所以你需要将二进制数读取为字符串,然后将每个字符转换为数字:
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned long long int bin2dec(const char *string, const size_t size)
{
unsigned long long int bit, value = 0;
for(size_t index=0;index<size;index++)
{
// moving from the end to the beginning, get a character from the string
// and convert it from a character containing a digit to a number
bit = string[size-index-1]-'0';
// in the original question this was: value += bit*pow(2,index);
// but we can just do this and get the same effect
// without multiplication or library function
value += bit<<index;
}
return value;
}
int main()
{
const char * binary = "111111111111111111111111111111";
unsigned long long int decimal = bin2dec(binary, strlen(binary));
printf("%llu\n",decimal);
return 0;
}
您不需要所有的索引和添加。您可以简单地从右边移入位:
#include <stdio.h>
#include <string.h>
unsigned long long int bin2dec(const char *string)
{
unsigned long long int value = 0;
while (*string != '[=10=]')
{
// make room for the next bit by shifting what is there already
value <<= 1;
// *string != '0' gives 1 if the current character is not '0', else 0
value |= *string != '0';
string++;
}
return value;
}
int main(void)
{
// 7 F C F F 4 F A F F F
const char * binary = "1111111110011111111010011111010111111111111";
unsigned long long int decimal = bin2dec(binary);
printf("%llX\n", decimal);
return 0;
}
我试过使用 C 制作正二进制到十进制数的转换器,但是当我尝试输入高于 1110011010110001010111(十进制为 3779671)的值时,程序总是 returns 那个确切的数字。我当前的作业要求它适用于最大为 11111111111111111111111111111 (1073741823) 的二进制数。
到目前为止,我已经尝试将变量类型更改为任何其他可能的更大尺寸,但它似乎不起作用。这是当前代码:
#include <math.h>
void main()
{
unsigned long long int bi, de = 0;
unsigned long long int x = 0, bases;
scanf("%llu", &bi);
for(x=0 ; bi>0 ; x++, bi=bi/10){
bases = bi % 10;
de = de + bases * pow(2,x);
}
printf("%llu", de); // imprime o correspondente em decimal
}
提前感谢您的帮助。
你不能读取二进制数11111111111111111111111111111并放入unsigned long long integer中,因为unsigned long long int的极限是18446744073709551615,所以你需要将二进制数读取为字符串,然后将每个字符转换为数字:
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned long long int bin2dec(const char *string, const size_t size)
{
unsigned long long int bit, value = 0;
for(size_t index=0;index<size;index++)
{
// moving from the end to the beginning, get a character from the string
// and convert it from a character containing a digit to a number
bit = string[size-index-1]-'0';
// in the original question this was: value += bit*pow(2,index);
// but we can just do this and get the same effect
// without multiplication or library function
value += bit<<index;
}
return value;
}
int main()
{
const char * binary = "111111111111111111111111111111";
unsigned long long int decimal = bin2dec(binary, strlen(binary));
printf("%llu\n",decimal);
return 0;
}
您不需要所有的索引和添加。您可以简单地从右边移入位:
#include <stdio.h>
#include <string.h>
unsigned long long int bin2dec(const char *string)
{
unsigned long long int value = 0;
while (*string != '[=10=]')
{
// make room for the next bit by shifting what is there already
value <<= 1;
// *string != '0' gives 1 if the current character is not '0', else 0
value |= *string != '0';
string++;
}
return value;
}
int main(void)
{
// 7 F C F F 4 F A F F F
const char * binary = "1111111110011111111010011111010111111111111";
unsigned long long int decimal = bin2dec(binary);
printf("%llX\n", decimal);
return 0;
}