[英]c++ decimal to binary
I am a beginner starting out in c++ and I am trying to turn a decimal byte into a binary number. 我是从C ++开始的初学者,我试图将十进制字节转换为二进制数。 However, there is something wrong with my syntax or logic but I cannot figure it out. 但是,我的语法或逻辑有问题,但我无法弄清楚。 When trying to debug, I believe the error is around userValue5 but I'm not sure why. 尝试调试时,我相信错误与userValue5有关,但我不确定为什么。 Any tips are appreciated and I am using VS2015. 感谢任何技巧,我正在使用VS2015。
#include "stdafx.h"
#include <iostream>
#include <stdint.h>
#include <cmath>
//finds where each column is a 0 or a 1
int binaryDigit(uint16_t y, uint16_t power)
{
if ((y / (pow(2, power))) > 1)
return 1;
else
return 0;
}
int difference(uint16_t y, int x, uint16_t power)
{
if (x == 1)
return y - pow(2, power);
else
return y;
}
//takes a decimal byte and turns it into binary
int main()
{
using namespace std;
cout << "Please insert a number between 0 and 255 so that I can convert it to binary: ";
uint16_t userValue(0), power(7);
cin >> userValue;
int firstDigit = binaryDigit(userValue, power);
uint16_t userValue2 = difference(userValue, firstDigit, power);
--power;
int secondDigit = binaryDigit(userValue2, power);
uint16_t userValue3 = difference(userValue2, secondDigit, power);
--power;
int thirdDigit = binaryDigit(userValue3, power);
uint16_t userValue4 = difference(userValue3, thirdDigit, power);
--power;
int fourthDigit = binaryDigit(userValue4, power);
uint16_t userValue5 = difference(userValue4, thirdDigit, power);
--power;
int fifthDigit = binaryDigit(userValue5, power);
uint16_t userValue6 = difference(userValue5, thirdDigit, power);
--power;
int sixthDigit = binaryDigit(userValue6, power);
uint16_t userValue7 = difference(userValue6, thirdDigit, power);
--power;
int seventhDigit = binaryDigit(userValue7, power);
uint16_t userValue8 = difference(userValue7, thirdDigit, power);
--power;
int eigthDigit = binaryDigit(userValue8, power);
cout << "The number " << userValue << " in binary is ";
cout << firstDigit << secondDigit << thirdDigit << fourthDigit << " " << fifthDigit << sixthDigit << seventhDigit << eigthDigit << endl;
return 0;
}
bitset
is your friend. bitset
是你的朋友。
#include <bitset>
#include <iostream>
using namespace std;
int main()
{
int userValue = 0;
cout << "Please insert a number between " << INT_MIN << " and " << INT_MAX << " so that I can convert it to binary: ";
cin >> userValue;
cout << bitset<32>(userValue) << endl;
return 0;
}
However, if you want to convert it to a string, you'll probably need stringstream
: 但是,如果要将其转换为字符串,则可能需要stringstream
:
stringstream ss;
ss << bitset<32>(userValue);
string str = ss.str();
I figured it out. 我想到了。 When I copied and pasted the same code to save time, I forgot to edit the arguments so that's why it wasn't working properly. 当我复制并粘贴相同的代码以节省时间时,我忘记了编辑参数,因此这不能正常工作。 I fixed it and it works great. 我修复了它,效果很好。 Thank you to everyone who commented and posted things like the table of exponents and the bit masking. 感谢所有发表评论并发布指数表和位掩码等内容的人。 I've learned a lot and cannot wait to write more programs. 我学到了很多东西,迫不及待想写更多的程序。
bitset is the way to go, but in general, maybe this is more helpful... 位集是要走的路,但总的来说,这可能更有用...
std::string intToBinaryString(int x)
{
// a useful class that allows easy concatenation
std::stringstream ss;
// create an integer that only has a 1 bit in its top position.
unsigned int mask = 1 << (sizeof(int) * 8 - 1);
// for each bit in x, starting at the top, check to see if its non zero...
for(int i = 0; i <sizeof(int) * 8; ++i)
{
// see if x's (top - i)'th bit is non zero
bool nonZeroBit = x & mask;
// assign the (top - i)'th "bit" of ss to be '1' if non zero and '0' otherwise
ss << (nonZeroBit ? '1' : '0');
// shift the mask down by 1.
mask >>= 1;
}
// What we get on the first iteration is
//
// mask = 10000...0000
// & x = 10101...0010
// -----------------------------
// nonZero = 10000...0000 = true
//
// the second iteration is
//
// mask = 01000...0000
// & x = 10101...0010
// -----------------------------
// nonZero = 00000...0000 = false
//
// the third iteration is
//
// mask = 00100...0000
// & x = 10101...0010
// -----------------------------
// nonZero = 00100...0000 = true
//
// ...
//
// then we just add these results into ss
// finally, convert it into a normal string
return ss.str();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.