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

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

问题描述

我想为十进制转换为二进制,如192 11000000.我只需要一些简单的code要做到这一点,但code我至今不工作:

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] = '\0';
    for (i = 4; i >= 0; --i, value >>= 1)
    {
        output[i] = (value & 1) + '0';
    }
}

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

推荐答案

的值不是小数。在计算机内存中的所有值都是二进制的。

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

你正在尝试做的是INT转换为使用特定基地的字符串。
有一个函数,该函数,这就是所谓的 itoa
<一href=\"http://www.cplusplus.com/reference/cstdlib/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天全站免登陆