# Converting from Decimal To Binary

I was on StackOverflow the other day and saw a question posed about how one might convert from Decimal to Binary, when the initial information is stored in a string. It seemed like a fun little program to take a whack at, so I did. I’ve posted my answer as well as the code solution below:

Image obtained here:

http://pictures.4ever.eu/cartoons/binary-code-161219

The original question and my answer can be found here:

http://stackoverflow.com/questions/34381002/is-there-a-way-to-convert-a-number-represented-as-a-string-to-its-binary-equiv/34381419#34381419

————————————————————-

Okay let’s break down the process you require here. (only one of an infinite number of ways to do this)

1) Conversion of a number represented as a string type into an integer type.

2) Conversion of the intermediary integer type into a binary number which is held in another string type. (judging by the return type of your function, which could just as easily return an integer by the way and save the headache of representing the binary equivalent as a string)

For step 1: Use the standard library function stoi. It does what you might imagine, extracts the numerical data from the string and stores it in an integer.

http://www.cplusplus.com/reference/string/stoi/

``````std::string numberstr = "123";
int numberint = std::stoi(numberstr);
std::cout << numberint << "\n";``````

Now you have the number as an integer.

For step 2:

1) This process involves the conversion of a number from base 10 (decimal) to base 2 (binary).

2) Divide the number by 2.

3) Store the remainder and the quotient of this division operation for further use.

4) The remainder becomes part of the binary representation, while the quotient is used as the next dividend.

5) This process repeats until the dividend becomes 1, at which point it too is included in the binary representation.

6) Reverse the string, and voila! You now have the binary representation of a number.

7) If you want to handle negative numbers (which I imagine you might), simply perform a check before the conversion to see if the converted integer is negative, and set a flag to true if it is.

8) Check this flag before reversing, and add a negative sign to end of the string before reversing.

The final function looks like this:

``````std::string str_to_bin(const std::string& str)
{
std::string binarystr = ""; // Output string

int remainder;
int numberint = std::stoi(str);
bool flagnegative = false;
// If negative number, beginning of binary equivalent is 1
if (numberint < 0)
{
numberint = abs(numberint);
flagnegative = true;
}
// If number is 0, don't perform conversion simply return 0
if (numberint == 0)
{
binarystr = "0";
return binarystr;
}
std::cout << numberint << "\n";

while (numberint != 1)
{
remainder = numberint % 2;
numberint /= 2;
std::ostringstream convert; // stream used for the conversion
convert << remainder;      // insert the textual representation of 'remainder' in the characters in the stream
binarystr += convert.str();
}
std::ostringstream final;
final << numberint;         // To insert the last (or rather first once reversed) binary number
binarystr += final.str();
if (flagnegative == true)
binarystr += "-";
std::reverse(binarystr.begin(), binarystr.end());
return binarystr;
}

``````

# OSVR HDK Unboxing

So I finally got my hands on the OSVR headset from Razer. I’m not going to lie, it took its sweet time getting here (about 2 months more than the initially projected date) but the sheer degree of awesome that is possessed by a VR headset helped negate all my irritation from the delay.

In other words, I was squealing like a child when I picked it up and began the unboxing. I hope my excitement translates to you, gentle reader!

About now is when the squealing reached supersonic levels.

The HDK itself seems to be divided into:

2. HDMI cables for the headset
3. Some sort of audio cables that I haven’t yet figured out how to use
4. The power supply
5. The positional tracking kit (i.e. the Camera, a tripod stand and relevant cabling)
6. A cleaning brush
7. A hub for all the cables to connect into

And there you have it!
The OSVR HDK unboxed.

The link below is the official GitHub repository of the OSVR project, and contains all the instructions I needed to get everything setup and test the headset out in an actual VR demo.

Suffice to say, I was blown away.

https://github.com/OSVR

AND

https://github.com/OSVR/OSVR-General/wiki/HDK-Unboxing-and-Getting-Started

I hope this was of help/interest to anyone looking for more info on the OSVR project.

Feel free to contact me at Nightmask3@gmail.com for any help or insight I can provide on being a part of this ambitious new venture!

Thanks!

Happy hacking.