C语言 C 库上的十进制到二进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13099252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:13:00  来源:igfitidea点击:

Decimal to Binary on C library

cbinary

提问by Tiago Santos

I want to know if there is a function in C library that convert a decimal to binary number and save number by number in a position on an array. For example: 2 -> 10 -> array [0] = 0 array[1] = 1. Thanks.

我想知道 C 库中是否有一个函数可以将十进制数转换为二进制数并逐个保存在数组上的某个位置。例如:2 -> 10 -> array [0] = 0 array[1] = 1。谢谢。

回答by Aniket Inge

here:

这里:

void dec2bin(int c)
{
   int i = 0;
   for(i = 31; i >= 0; i--){
     if((c & (1 << i)) != 0){
       printf("1");
     }else{
       printf("0");
     } 
   }
}

But this only prints the value of an integer in binary format. All data is represented in binary format internally anyway.

但这仅以二进制格式打印整数的值。无论如何,所有数据在内部都以二进制格式表示。

回答by md5

There is no such function in C standard library. Anyway, you can write your own:

C 标准库中没有这样的函数。无论如何,您可以编写自己的:

void get_bin(int *dst, intmax_t x);

Where dstis the resulting array (with 1s and 0s), and xis the decimal number.

哪里dst是结果数组(带有1s 和0s),x是十进制数。

For example:

例如:

C89 version:

C89版本:

#include <limits.h>

void get_bin(int *dst, int x)
{
    int i;

    for (i = sizeof x * CHAR_BIT - 1; i >= 0; --i)
        *dst++ = x >> i & 1;
}

C99 version:

C99版本:

/* C99 version */
#include <limits.h>
#include <stdint.h>

void get_bin(int *dst, intmax_t x)
{
    for (intmax_t i = sizeof x * CHAR_BIT - 1; i >= 0; --i)
        *dst++ = x >> i & 1;
}

It works as follow: we run through the binary representation of x, from left to right. The expression (sizeof x * CHAR_BIT - 1)give the number of bits of x- 1. Then, we get the value of each bit (*dst++ = x >> i & 1), and push it into the array.

它的工作原理如下:我们x从左到右遍历 的二进制表示。该表达式(sizeof x * CHAR_BIT - 1)给出了x- 1的位数。然后,我们得到每个位 ( *dst++ = x >> i & 1)的值,并将其压入数组。

Example of utilisation:

使用示例:

void get_bin(int *dst, int x)
{
    int i;

    for (i = sizeof x * CHAR_BIT - 1; i >= 0; --i)
        *dst++ = x >> i & 1;
}

int main(void)
{
    int buf[128];       /* binary number */
    int n = 42;         /* decimal number */
    unsigned int i;

    get_bin(buf, n);

    for (i = 0; i < sizeof n * CHAR_BIT; ++i)
        printf("%d", buf[i]);

    return 0;
}

回答by Basile Starynkevitch

You did not define what is a decimal number for you. I am guessing it is character representation (e.g. in ASCII) of that number.

您没有为您定义十进制数。我猜它是那个数字的字符表示(例如在 ASCII 中)。

Notice that numbers are just numbers. Binary or decimal numbers do not exist, but a given number may have a binary, and a decimal, representation. Numbers are not made of digits!

请注意,数字只是数字。二进制或十进制数不存在,但给定的数可能有二进制和十进制表示。数字不是由数字组成的!

Then you probably want sscanf(3)or strtol(3)pr atoito convert a string to an integer (e.g. an intor a long), and sprintf(3)to convert an integer to a string.

那么您可能希望sscanf(3)strtol(3)pratoi将字符串转换为整数(例如 anint或 a long),以及sprintf(3)将整数转换为字符串。

If you want to convert a number to a binary string (with only 0or 1char-s in it) you need to code that conversion by yourself. To convert a binary string to some longuse strtol.

如果要将数字转换为二进制字符串(其中仅包含01char-s),则需要自己编写该转换的代码。要将二进制字符串转换为某些long使用strtol.

回答by kAmol

Use char * itoa ( int value, char * str, int base );Find more here...

使用在此处char * itoa ( int value, char * str, int base );查找更多...

回答by Djordje Zivanovic

the function should go like this:

该功能应该是这样的:

int dec2bin(int n){
  static int bin,osn=1,c;
  if(n==0) return 0;
  else {
    c=n%2;
    bin += c*osn;
    osn*=10;
    dec2bin(n/2);
  }
  return bin;
}

回答by GeneralSmrdiGuz

As far as i know there is no such function in any C library. But here's a recursive function that returns a binary representation of a decimal number as int:

据我所知,任何 C 库中都没有这样的函数。但这里有一个递归函数,它返回一个十进制数的二进制表示为 int:

int dec2bin(int n)
{
    if(n == 0) return 0;
    return n % 2 + 10 * dec2bin(n / 2);
}

The max number that it can represent is 1023 (1111111111 in binary) because of intdata type limit, but you can substitute intfor long longdata type to increase the range. Then, you can store the return value to array like this:

由于int数据类型限制,它可以表示的最大数为 1023(二进制为 1111111111),但您可以用int代替long long数据类型以增加范围。然后,您可以像这样将返回值存储到数组中:

int array[100], i = 0;
int n = dec2bin(some_number);

do{
    array[i] = n % 10;
    n /= 10;
    i++;
}while(n > 10)

I know this is an old post, but i hope this will still help somebody!

我知道这是一个旧帖子,但我希望这仍然会对某人有所帮助!