Strings decimal to binary

okay lets say i have the following String and inside it there is a decimal number.
How do I translate it to binary number. I know its easier to declare an integer
but its for a project that I have to use a string.Also how can i check if it is
a number.
1
2
3
//how would i make this a binary ?
std::string instruction = "3";
closed account (jvqpDjzh)
You can use this ToNumber template function that uses the std::istringstream class to convert a string to a number. As I understood, it always tries to convert the number, for example, if you have an expression like this: "231a2" it returns the number 231, which means that this functions seems to return the number until it doesn't find a letter or something else different from a number. If you insert "a123", it returns 0.
1
2
3
4
5
6
7
template <class N> N ToNumber(std::string string_number)
{
    N number;
    std::istringstream iss(string_number);
    iss >> number;
    return number;
}

I have written this function a time ago to convert from decimal to binary numbers, which uses another function that I wrote (ReverseString)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//converts a integer number to a binary number represented by a string.
//this function uses 2 other functions: ToString() and reverse_string()
std::string DecimalToBinary(int decimal)
{
    int coefficient = -1, numerator = decimal;
    std::string binary = "";

    while(coefficient != 0)
    {
        binary += ToString(numerator % 2);//ToString() defined above
        coefficient = numerator / 2;
        numerator = coefficient;
    }
    return ReverseString(binary);//defined below
}

1
2
3
4
5
6
7
8
//reverse string digits
std::string ReverseString(std::string str)
{
    std::string new_string = "";
    for(int i=(str.length()-1); i>=0; i--)
        new_string += str.at(i);
    return new_string;
}

Please make me know if there bugs: I have written them when I was a bit tired, lol

EDIT:Here you have also the ToString one that I use in DecimalToBinary() one
1
2
3
4
5
6
7
template <class N> std::string ToString(N number)
{
    std::ostringstream oss;
    oss << number;
    std::string str = oss.str();
    return str;
}
Last edited on
Also the built-in bitset can be used to convert to/from binary
1
2
3
    unsigned long num = 3;
    std::bitset<32> bits(num);
    std::string binary = bits.to_string();


Also how can i check if it is a number.
1
2
3
4
5
6
7
    std::string instruction = "3";
    std::istringstream ss(instruction);
    unsigned long num;
    if (ss >> num)
    {
     // yes we have a number
    }
Last edited on
Okay so Now I am able to check if its a number thank u very much . however still not sure how to convert to a binary string since the above function zwilu gave me std::string DecimalToBinary(int decimal) takes an int as an argument and i needed to take a string.
I Aslo have another fucntion:
1
2
3
4
5
6
7
8
9
10
11
12
void binary(int number) {
	int remainder;

	if(number <= 1) {
		cout << number;
		return;
	}

	remainder = number%2;
	binary(number >> 1);    
	cout << remainder;
}

this one however doesn't transform my string to binary and also takes an int with as an argument.
The second block of code I gave converts a string to an integer (unsigned long is a type of integer).
Topic archived. No new replies allowed.