Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a decimal string to binary string?

I have a decimal string like this (length < 5000):

std::string decimalString = "555";

Is there a standard way to convert this string to binary representation? Like this:

std::string binaryString = "1000101011";

Update.

This post helps me.

like image 524
Denis Avatar asked Mar 20 '23 22:03

Denis


1 Answers

As the number is very large, you can use a big integer library (boost, maybe?), or write the necessary functions yourself.

If you decide to implement the functions yourself, one way is to implement the old pencil-and-paper long division method in your code, where you'll need to divide the decimal number repeatedly by 2 and accumulate the remainders in another string. May be a little cumbersome, but division by 2 should not be so hard.

like image 171
Sufian Latif Avatar answered Mar 28 '23 12:03

Sufian Latif