十进制到二进制转换
Decimal to Binary Conversion
我正在编写一个函数,用于在十进制和二进制基数系统之间进行转换,下面是我的原始代码:
void binary(int number)
{
vector<int> binary;
while (number == true)
{
binary.insert(binary.begin(), (number % 2) ? 1 : 0);
number /= 2;
}
for (int access = 0; access < binary.size(); access++)
cout << binary[access];
}
它没有工作,直到我这样做:
while(number)
怎么了?while(number == true)
,这两种形式有什么区别?
当您输入while (number)
时,number
(即int
)被转换为bool
类型。如果为零,则为false
,如果为非零,则为true
。
当你说while (number == true)
时,true
被转换为int
(成为1
),它就像你说while (number == 1)
一样。
下面是我的代码....
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<unistd.h>
#include<assert.h>
#include<stdbool.h>
#define max 10000
#define RLC(num,pos) ((num << pos)|(num >> (32 - pos)))
#define RRC(num,pos) ((num >> pos)|(num << (32 - pos)))
void tobinstr(int value, int bitsCount, char* output)
{
int i;
output[bitsCount] = ' ';
for (i = bitsCount - 1; i >= 0; --i, value >>= 1)
{
output[i] = (value & 1) + '0';
}
}
int main()
{
char s[50];
tobinstr(65536,32, s);
printf("%sn", s);
return 0;
}
相关文章:
- visual在c++中将十进制数转换为二进制数
- C++十进制到二进制,如何转换
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- C++从二进制转换为十进制数
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 如何在 O(n) 中将二进制和转换为二进制
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 十进制到二进制转换器 c++
- 在C++中将结构转换和存储为二进制
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 如何在C ++中将二进制字符串128位转换为十进制字符串?
- 如何将二进制文件头示例代码 (C++/C#) 转换为 Python?
- 二进制 '==':未找到采用 'Enemy' 类型左侧操作数的运算符(或者没有可接受的转换)
- 将二进制转换为十进制,我如何要求用户仅输入二进制
- QTextStream的操作数到二进制的转换无效
- 在c++中读取和转换二进制数据
- c++转换二进制到十进制从字符串输入
- 计算器转换二进制到浮点值-我做错了什么