用C语言将十进制转换成二进制
我正在尝试将十进制数转换为二进制数,例如192到11000000。我只需要一些简单的代码就可以做到这一点,但到目前为止我的代码不起作用:用C语言将十进制转换成二进制,c,binary,decimal,C,Binary,Decimal,我正在尝试将十进制数转换为二进制数,例如192到11000000。我只需要一些简单的代码就可以做到这一点,但到目前为止我的代码不起作用: void dectobin(int value, char* output) { int i; output[5] = '\0'; for (i = 4; i >= 0; --i, value >>= 1) { output[i] = (value & 1) + '0'; } }
void dectobin(int value, char* output)
{
int i;
output[5] = '\0';
for (i = 4; i >= 0; --i, value >>= 1)
{
output[i] = (value & 1) + '0';
}
}
任何帮助都将不胜感激 5位数对于您的示例(192)是不够的。可能您应该增加
输出
该值不是十进制的。计算机内存中的所有值都是二进制的
您试图做的是使用特定的基将int转换为字符串。
这里有一个函数,叫做itoa
。
看起来是这样的,但要小心,必须反转生成的字符串:-)
#包括
#包括
#包括
字符输出[256]=“”;
int main()
{
int x=192;
int n;
n=x;
INTR;
做{
r=n%2;
如果(r==1)
strcat(输出,“1”);
其他strcat(输出,“0”);
n=n/2;
}
而(n>0);
printf(“%s\n”,输出);
}
也许理解算法可以让您编写或修改自己的代码以满足您的需要。我确实看到您没有足够的字符数组长度来显示192的二进制值(您需要8位二进制,但您的代码只提供5位二进制)
下面是一个例子,它清楚地解释了算法
我不是一个C/C++程序员,所以这里是我基于算法示例的C#代码贡献
int I = 0;
int Q = 95;
string B = "";
while (Q != 0)
{
Debug.Print(I.ToString());
B += (Q%2);
Q = Q/2;
Debug.Print(Q.ToString());
I++;
}
Debug.Print(B);
所有的Debug.Print只是为了显示输出。所以。。。您是否检查了代码的输出以了解它为什么不工作
So iteration 1 of your loop:
value = 192
i = 4
output[i] = (11000000 & 1) + '0' = 0 + 48 = 48 (char `0`)
Iteration 2 of your loop:
value = 96
i = 3
output[i] = (1100000 & 1) + '0' = 0 + 48 = 48 (char `0`)
Iteration 3 of your loop:
value = 48
i = 2
output[i] = (110000 & 1) + '0' = 0 + 48 = 48 (char `0`)
Iteration 4 of your loop:
value = 24
i = 1
output[i] = (11000 & 1) + '0' = 0 + 48 = 48 (char `0`)
Iteration 5 of your loop:
value = 12
i = 0
output[i] = (1100 & 1) + '0' = 0 + 48 = 48 (char `0`)
Final string: "00000" and you wanted: "11000000"
你的代码有问题吗?不。我也不知道,你只是走得不够远。将输出/循环更改为:
output[8] = '\0';
for (i = 7; i >= 0; --i, value >>= 1)
然后返回正确的结果
我只推荐一种更通用的方法,您使用的是固定长度的字符串,它将您限制为一定长度的二进制数。您可能希望执行以下操作:
loop while number dividing down is > 0
count number of times we loop
malloc an array the correct length and be returned
几天前,我在搜索快速且可移植的sprintf(“%d”,num)。在页面上找到此实现:
/**
*C++版本0.4 char *风格“ITOA”:
*卢卡斯·奇梅拉写
*根据GPLv3发布。
*/
字符*itoa(int值、字符*结果、int基){
//检查基座是否有效
if(base<2 | | base>36){*result='\0',返回result;}
char*ptr=result,*ptr1=result,tmp_char;
int tmp_值;
做{
tmp_值=值;
值/=基数;
*ptr++=“ZYXWVUTSRQPONLKJIHGFEDCBA9876543210123456789ABCDEFGHIJKLMNOPQRSTUVXYZ”[35+(tmp_值-值*基)];
}而(价值);
//应用负号
如果(tmp_值<0)*ptr++='-';
*ptr--='\0';
while(ptr1
#包括
#包括
空箱(整数){
int n=num;
char*s=malloc(sizeof(int)*8);
int i,c=0;
printf(“%d\n”,num);
对于(i=sizeof(int)*8-1;i>=0;i--){
n=num>>i;
*(s+c)=(n&1)‘1’:‘0’;
C++;
}
*(s+c)=空;
printf(“%s”,s);//或者您也可以返回字符串s,然后在需要时释放它
}
int main(int argc,char*argv[]){
bin(atoi(argv[1]);
返回退出成功;
}
您也可以使用函数下的while循环来完成。我只是在搜索我的解决方案,但我得到的解决方案不合适,因此我采用了相应的实用方法(使用2除以,直到得到0,并将提醒存储在一个数组中),并打印数组的反面,然后
#包括
int main()
{
长整型a,c;
int i=0,count=0;
char-bol[10000];
scanf(“%lld”、&a);
c=a;
while(a!=0)
{
bol[i]=a%2;
a=a/2;
计数++;
i++;
}
如果(c==0)
{
printf(“0”);
}
其他的
{
对于(i=count-1;i>=0;i--)
{
printf(“%d”,bol[i]);
}
}
printf(“\n”);
返回0;
}
首先,192
不能用4
位表示
192=1100 0000
,需要最少8位
这里有一个简单的例子
#包括
#包括
int main()
{
长十进制,临时十进制;
字符二进制[65];
int指数=0;
/*
*从用户读取十进制数
*/
printf(“输入任何十进制值:”);
scanf(“%ld”&十进制);
/*将十进制值复制到临时变量*/
tempDecimal=十进制;
while(tempDecimal!=0)
{
/*查找小数点%2并将其添加到二进制值*/
二进制[索引]=(临时十进制%2)+“0”;
tempDecimal/=2;
索引++;
}
二进制[索引]='\0';
/*反转找到的二进制值*/
strev(二进制);
printf(“\n十进制值=%ld\n”,十进制);
printf(“十进制的二进制值=%s”,二进制);
返回0;
}
用C语言将十进制转换为二进制
#包括
void main()
{
长整数n,n1,m=1,rem,ans=0;
printf(“\n输入小数点编号(0到1023之间):”;
scanf(“%ld”、&n);
n1=n;
而(n>0)
{
rem=n%2;
ans=(rem*m)+ans;
n=n/2;
m=m*10;
}
printf(“\n您的十进制编号为::%ld”,n1);
printf(“\n转换为二进制编号为::%ld”,ans);
}
//使用堆栈将十进制转换为二进制的C程序
#include<stdio.h>
#define max 100
int stack[max],top=-1,i,x;
void push (int x)
{
++top;
stack [top] = x;
}
int pop ()
{
return stack[top];
}
void main()
{
int num, total = 0,item;
print f( "Please enter a decimal: ");
scanf("%d",&num);
while(num > 0)
{
total = num % 2;
push(total);
num /= 2;
}
for(i=top;top>-1;top--)
{
item = pop ();
print f("%d",item);
}
}
#包括
#定义最大值100
int stack[max],top=-1,i,x;
无效推送(整数x)
{
++顶部;
堆栈[顶部]=x;
}
int-pop()
{
返回堆栈[顶部];
}
void main()
{
int num,总计=0,项目;
打印f(“请输入小数:”);
scanf(“%d”和&num);
while(num>0)
{
总数=num%2;
推送(总);
num/=2;
}
对于(i=top;top>-1;top--)
{
item=pop();
打印f(“%d”,项目);
}
}
以下是将十进制数转换为二进制数的算法
- 划分
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char* itoa(int value, char* result, int base) {
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
#include <stdio.h>
#include <stdlib.h>
void bin(int num) {
int n = num;
char *s = malloc(sizeof(int) * 8);
int i, c = 0;
printf("%d\n", num);
for (i = sizeof(int) * 8 - 1; i >= 0; i--) {
n = num >> i;
*(s + c) = (n & 1) ? '1' : '0';
c++;
}
*(s + c) = NULL;
printf("%s", s); // or you can also return the string s and then free it whenever needed
}
int main(int argc, char *argv[]) {
bin(atoi(argv[1]));
return EXIT_SUCCESS;
}
#include <stdio.h>
int main()
{
long long int a,c;
int i=0,count=0;
char bol[10000];
scanf("%lld", &a);
c = a;
while(a!=0)
{
bol[i] = a%2;
a = a / 2;
count++;
i++;
}
if(c==0)
{
printf("0");
}
else
{
for(i=count-1; i>=0; i--)
{
printf("%d", bol[i]);
}
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
long decimal, tempDecimal;
char binary[65];
int index = 0;
/*
* Reads decimal number from user
*/
printf("Enter any decimal value : ");
scanf("%ld", &decimal);
/* Copies decimal value to temp variable */
tempDecimal = decimal;
while(tempDecimal!=0)
{
/* Finds decimal%2 and adds to the binary value */
binary[index] = (tempDecimal % 2) + '0';
tempDecimal /= 2;
index++;
}
binary[index] = '\0';
/* Reverse the binary value found */
strrev(binary);
printf("\nDecimal value = %ld\n", decimal);
printf("Binary value of decimal = %s", binary);
return 0;
}
#include<stdio.h>
void main()
{
long int n,n1,m=1,rem,ans=0;
printf("\nEnter Your Decimal No (between 0 to 1023) :: ");
scanf("%ld",&n);
n1=n;
while(n>0)
{
rem=n%2;
ans=(rem*m)+ans;
n=n/2;
m=m*10;
}
printf("\nYour Decimal No is :: %ld",n1);
printf("\nConvert into Binary No is :: %ld",ans);
}
#include<stdio.h>
#define max 100
int stack[max],top=-1,i,x;
void push (int x)
{
++top;
stack [top] = x;
}
int pop ()
{
return stack[top];
}
void main()
{
int num, total = 0,item;
print f( "Please enter a decimal: ");
scanf("%d",&num);
while(num > 0)
{
total = num % 2;
push(total);
num /= 2;
}
for(i=top;top>-1;top--)
{
item = pop ();
print f("%d",item);
}
}
int main()
{
int n, c, k;
printf("Enter an integer in decimal number system: ");
scanf("%d", &n);
printf("%d in binary number system is: ", n);
for (c = n; c > 0; c = c/2)
{
k = c % 2;//To
k = (k > 0) ? printf("1") : printf("0");
}
getch();
return 0;
}
//decimal to binary converter
long int dec2bin(unsigned int decimal_number){
if (decimal_number == 0)
return 0;
else
return ((decimal_number%2) + 10 * dec2bin(decimal_number/2));
}
number=215
a=str(int(number//128>=1))+str(int(number%128>=64))+
str(int(((number%128)%64)>=32))+str(int((((number%12
8)%64)%32)>=16))+str(int(((((number%128)%64)%32)%16)>=8))
+str(int(((((((number%128)%64)%32)%16)%8)>=4)))
+str(int(((((((((number%128)%64)%32)%16)%8)%4)>=2))))
+str(int(((((((((((number%128)%64)%32)%16)%8)%4)%2)>=1)))))
print(a)
#include <stdio.h>
void main()
{
int n,i,j,sum=0;
printf("Enter a Decimal number to convert it to binary : ");
scanf("%d",&n);
for(i=n,j=1;i>=1;j*=10,i/=2)
sum+=(i%2)*j;
printf("\n%d",sum);
}