C++ 如何读取二进制数作为输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25441364/
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-08-28 11:19:10  来源:igfitidea点击:

How to read a binary number as input?

c++cinputuser-inputc++14

提问by iec2011007

Is there a way for the user to input a binary number in C or C++?

有没有办法让用户在 C 或 C++ 中输入二进制数?

If we write something like

如果我们写这样的东西

int a = 0b1010;
std::cout << a << std::endl

Then the output comes out to be 10 (when using the appropriate compiler extensions).

然后输出为 10(使用适当的编译器扩展时)。

but when we try to write

但是当我们尝试写

int n;
std::cin >> n;
int t = 0bn;

It gives us an error so can anyone suggest that how can we directly read binary number as input rather than using string to store input?

它给了我们一个错误,所以任何人都可以建议我们如何直接读取二进制数作为输入而不是使用字符串来存储输入?

采纳答案by Alexander Gessler

There is a bit of confusion here, let's disentangle it a bit.

这里有点混乱,让我们稍微解开一下。

  • 0b1010is an integer literal, a constant, compile-time integer value written in base 2. Likewise, 0xAis a literal in base 16 and 10is in base 10. All of these refer to the same integer, it is just a different way of telling the compiler which number you mean. At runtime, in memory, this integer is alwaysrepresented as a base-2 number.

  • std::cout << a; takes the integer value of aand outputs a string representation of it. By default it outputs it in base 10, but you can i.e use the std::hexmodifier to have it output it in base 16. There is no predefined modifier to print in binary. So you need to do that on your own (or google it, it is a common question).

  • 0bat last, is onlyused to define integer literals. It is nota runtime operator. Recall, all ints are represented as base 2 numbers in memory. Other bases do not exist from a machine point of view, intis int, so there is nothing to convert. If you need to read a binary number from a string, you would roll the reverse code to what you do to print it (std::cin >> nassumes that the input is a base 10 number, so it reads a wrong number if the input is actually intended to be in base 2).

  • 0b1010是一个整数文字,一个常量,编译时以 20xA为底的整数值。同样,是一个以 1610为底的文字,以 10 为底。所有这些都是指同一个整数,这只是告诉编译器你的意思是哪个数字。在运行时,在内存中,此整数始终表示为以 2 为底的数字。

  • std::cout << a; 获取 的整数值a并输出它的字符串表示形式。默认情况下,它以基数 10 输出它,但您可以即使用std::hex修饰符让它以基数 16 输出它。没有预定义的修饰符以 binary 打印。所以你需要自己做(或谷歌它,这是一个常见的问题)。

  • 0b最后,用于定义整数文字。它不是运行时运算符。回想一下,所有ints 在内存中都表示为基数为 2 的数字。从机器的角度来看,其他基数不存在,intis int,因此没有什么可转换的。如果您需要从字符串中读取一个二进制数,您可以将反向代码滚动到打印它的内容(std::cin >> n假设输入是一个以 10 为基数的数字,因此如果输入实际上是要读取的数字,它会读取错误的数字在基地 2)。

回答by Klas Lindb?ck

While there is no function to read binary numbers directly, there are functions, strtox(where x represents the data type) to convert a string containing a binary number (or a number of any other base) to a numeric value.

虽然没有直接读取二进制数的函数,但有一些函数strtox(其中 x 表示数据类型)将包含二进制数(或任何其他基数)的字符串转换为数值。

So the solution is to first read the number as a string and then convert it.

所以解决办法是先把数字读成字符串,然后再进行转换。

Example:

例子:

char input[100];
char *endpointer;

<read input using either C or C++ syntax>

int n = (int) strtol(input, &endpointer, 2);

回答by bumfo

rather do it yourself:

而是自己做:

uint32_t a = 0;

char c;
while ((c = getchar()) != '\n') { // read a line char by char
  a <<= 1;                        // shift the uint32 a bit left
  a += (c - '0') & 1;             // convert the char to 0/1 and put it at the end of the binary
}

printf("%u\n", a);

回答by Genius

To take binary number as input, there are two ways I use frequently:

以二进制数作为输入,我常用的有两种方式:

(Key note: Take the input as string!!! use: #include<string>)

重点注:以输入作为字符串!使用方法:#include<string>

  1. The to_ulong()method of the bitsettemplate of the bitset library
    • for this you need to include the bitsetlibrary using #include<bitset>
  1. to_ulong()该方法bitset的位集库的模板
    • 为此,您需要使用包含bitset#include<bitset>

Example :

例子 :

string s;
cin>>s; // Suppose s = "100100101"
int n = (int) bitset<64>(s).to_ulong();
cout<<n; // 293

Explore more about bitsethereand about to_ulong()here.

探索更多关于bitset这里和关于to_ulong()这里的信息

  1. The stoi()method of the stringlibrary
    • for this you need to include the stringlibrary using #include<string>
  1. 图书馆 的stoi()方法string
    • 为此,您需要使用包含string#include<string>

Example :

例子 :

string s;
cin>>s; // Suppose s = "100100101"
int n = stoi(s, 0, 2);
cout<<n; // 293

Explore the format of stoi()here.

探索stoi()这里的格式。