C 32位二进制到十进制转换
我有一段从二进制到十进制的代码:C 32位二进制到十进制转换,c,recursion,binary,C,Recursion,Binary,我有一段从二进制到十进制的代码: #include <stdio.h> #include <math.h> #include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
int main() {
printf("%lld\n", binaryToDecimal(11110000111100001111000011110000));
return 1;
}
long long binaryToDecimal(long long binary) {
int power = 0;
return binaryToDecimalHelper(binary, power);
}
long long binaryToDecimalHelper(long long binary, int power) {
if (binary != 0) {
long long i = binary % (double)10;
return (i * pow(2, power))
+ binaryToDecimalHelper(binary / 10, power + 1);
} else {
return 0;
}
}
#包括
#包括
#包括
int main(){
printf(“%lld\n”,二进制数字(1111000011100001110000111110010000));
返回1;
}
long long binaryToDecimal(long long binary){
整数幂=0;
返回binaryToDecimalHelper(二进制,幂);
}
long long binaryToDecimalHelper(long long binary,整数幂){
如果(二进制!=0){
长i=二进制(双精度)10;
返回(i*pow(2,功率))
+binaryToDecimalHelper(二进制/10,幂+1);
}否则{
返回0;
}
}
对于小值(最多16位),它可以正常工作,但对于32位(我需要的是),它只返回垃圾 数字1111000011110000111100001111100000是int类型,在您的机器中不能容纳像1111000011110000111100001111100000这样大的数字。最好使用字符串表示法(“1111000011100001111001111000011110000”)并调整算法,如果可以的话。数字1111000011110000111110001110000是int类型的,它不能在计算机中容纳111100001111000011110000这样大的数字。最好改用字符串表示法(“1111000011100001111001111000011110000”)并调整算法(如果可以)。如果限制为最大32位,这是一个示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void setStr(char *c, const char * x)
{
int i = 0;
while(x[i] != '\0')
{
c[i] = x[i];
i++;
}
}
void prepend(char* s, const char* t)
{
size_t len = strlen(t);
size_t i;
memmove(s + len, s, strlen(s) + 1);
for (i = 0; i < len; ++i)
{
s[i] = t[i];
}
}
int main(int argc, char const *argv[])
{
char *x = malloc(33*sizeof(char));
setStr(x, "111");
while (strlen(x) < 31) // not really necessary, but will help to 'cut' bytes if necessary
{
prepend(x,"0");
}
printf("%s\n", x);
int i = strtol(x,NULL,2);
printf("%d\n",i);
free(x);
return 0;
}
#包括
#包括
#包括
void setStr(字符*c,常量字符*x)
{
int i=0;
而(x[i]!='\0')
{
c[i]=x[i];
i++;
}
}
无效预结束(字符*s,常量字符*t)
{
尺寸长度=标准长度(t);
尺寸i;
memmove(s+len、s、strlen(s)+1);
对于(i=0;i
如果限制为最大32位,这是一个示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void setStr(char *c, const char * x)
{
int i = 0;
while(x[i] != '\0')
{
c[i] = x[i];
i++;
}
}
void prepend(char* s, const char* t)
{
size_t len = strlen(t);
size_t i;
memmove(s + len, s, strlen(s) + 1);
for (i = 0; i < len; ++i)
{
s[i] = t[i];
}
}
int main(int argc, char const *argv[])
{
char *x = malloc(33*sizeof(char));
setStr(x, "111");
while (strlen(x) < 31) // not really necessary, but will help to 'cut' bytes if necessary
{
prepend(x,"0");
}
printf("%s\n", x);
int i = strtol(x,NULL,2);
printf("%d\n",i);
free(x);
return 0;
}
#包括
#包括
#包括
void setStr(字符*c,常量字符*x)
{
int i=0;
而(x[i]!='\0')
{
c[i]=x[i];
i++;
}
}
无效预结束(字符*s,常量字符*t)
{
尺寸长度=标准长度(t);
尺寸i;
memmove(s+len、s、strlen(s)+1);
对于(i=0;i
目前为止,使用字符串作为输入而不是int是最简单的,并且允许使用更长的数字。您的问题可能是由整数溢出引起的
字符串版本:
#include <math.h>
#include <stdio.h>
#include <string.h>
int main() {
const char * const string_to_convert = "1010"
int result = 0;
for( int i = strlen(string_to_convert) - 1; i >= 0; --i ) {
if( string_to_convert[i] == '1' ) {
// Careful with pow() -- returns double, may round incorrectly
result += (int)pow( 10.0d, (double)i )
}
}
fprintf( stdout, "%d", result );
return 0;
}
#包括
#包括
#包括
int main(){
const char*const string_to_convert=“1010”
int结果=0;
for(int i=strlen(字符串转换)-1;i>=0;--i){
if(字符串转换为[i]='1'){
//小心pow()--返回双精度,可能会不正确地取整
结果+=(整数)功率(10.0d,(双)i)
}
}
fprintf(标准输出,“%d”,结果);
返回0;
}
另外,我不确定返回1的点是什么。通常,main中的非零返回值表示错误。目前为止,使用字符串作为输入而不是int最简单,并且允许使用更长的数字。您的问题可能是由整数溢出引起的 字符串版本:
#include <math.h>
#include <stdio.h>
#include <string.h>
int main() {
const char * const string_to_convert = "1010"
int result = 0;
for( int i = strlen(string_to_convert) - 1; i >= 0; --i ) {
if( string_to_convert[i] == '1' ) {
// Careful with pow() -- returns double, may round incorrectly
result += (int)pow( 10.0d, (double)i )
}
}
fprintf( stdout, "%d", result );
return 0;
}
#包括
#包括
#包括
int main(){
const char*const string_to_convert=“1010”
int结果=0;
for(int i=strlen(字符串转换)-1;i>=0;--i){
if(字符串转换为[i]='1'){
//小心pow()--返回双精度,可能会不正确地取整
结果+=(整数)功率(10.0d,(双)i)
}
}
fprintf(标准输出,“%d”,结果);
返回0;
}
另外,我不确定返回1的点是什么。通常,main的非零返回值表示错误。首先要明确的是,您的代码不会将任何内容转换为十进制,而是转换为
int
。通过调用printf()
将该整数转换为十进制字符串表示形式
文字常量1111000011110000111110011001111000011110000由编译器解释为十进制值(如果不是那么大的话,也可以解释为十进制值),因此需要存储104位
i、 e.日志10(11110000111100000111100010)/日志10(210)
用只包含1和0位的十进制整数表示二进制值在数学上没有多大意义——尽管它可能是凸的