c++/c 十进制转2进制

#include <stdio.h>
////////////////////////////////////////////////


char* DecToBinary(int value, char* buffer, int len)
{
    int downLimit = min(sizeof(value)*8, len);

    for(int i = 0, j = downLimit-1; i < downLimit; ++i, --j)
    {
        buffer[j] = ( value & (1 << i) ) ? '1' : '0';
    }

    return buffer;
}

int main()
{
    int value = 2147516416;
    char buffer[32] = {0};

    printf("%s.\n", DecToBinary(value, buffer, 32));

    return 0;
}

只希望为你省几行代码....

    原文作者:进制转换
    原文地址: https://blog.csdn.net/oiooooio/article/details/43758911
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞