在C中将十进制转换为二进制 [英] Convert decimal to binary in C

查看:19
本文介绍了在C中将十进制转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将十进制转换为二进制,例如 192 到 11000000.我只需要一些简单的代码来执行此操作,但我目前的代码不起作用:

I am trying to convert a decimal to binary such as 192 to 11000000. I just need some simple code to do this but the code I have so far doesn't work:

void dectobin(int value, char* output)
{
    int i;
    output[5] = '';
    for (i = 4; i >= 0; --i, value >>= 1)
    {
        output[i] = (value & 1) + '0';
    }
}

任何帮助将不胜感激!

推荐答案

值不是十进制的.计算机内存中的所有值都是二进制的.

The value is not decimal. All values in computer's memory are binary.

您要做的是使用特定的基数将 int 转换为字符串.有一个函数,它叫做 itoa.http://www.cplusplus.com/reference/cstdlib/itoa/

What you are trying to do is to convert int to a string using specific base. There's a function for that, it's called itoa. http://www.cplusplus.com/reference/cstdlib/itoa/

这篇关于在C中将十进制转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆