-2

can anyone please help me with this code I wrote. I am multiplying the digit from the string that is either 0 or 1 by 2 to the power of "power" which is an integer incrementing every time we loop and I am adding the result to the return value...but for some reason it's not working.

So for example: "10" is returning 48 instead of 2

#include <iostream>
#include <string>
#include <cmath>
#include <sstream>

using BINARY = unsigned long;
typedef int BI;

class Binary_
{
private:
std::string binary_string;
BINARY return_binary()
{
    BINARY result = 0;
    BI power = 0, binary_value = 0;
    for(int i = this->binary_string.length() - 1; i > 0; i--)
    {
        std::string binary_char_hold = std::to_string(this->binary_string[i]);
        std::stringstream Binary_stream(binary_char_hold);
        Binary_stream >> binary_value;
        result = result + (binary_value * pow(2, power));
        power++;
    }
    return result;
 }
 public :
    BINARY get_binary(std::string binary_string)
    {
        BINARY binary_val = 0;
        for (int i = 0; i < binary_string.length(); i++)
        {
            if (binary_string[i] == '0' || binary_string[i] == '1') { this->binary_string = 
            binary_string; binary_val = return_binary(); }
            else this->binary_string = "Binary value you entered is invalid and not of base 2 system!";
        }
        return binary_val;
    }
   };
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • The code does a binary to decimal conversion, not the other way around as stated in the title. Please [edit] your question to fix it. Also please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mre]. – Some programmer dude Feb 10 '22 at 19:38
  • As for your problem, now might be a good time to learn how to use a *debugger* to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Feb 10 '22 at 19:39
  • 3
    Values of any type (`int`, `unsigned long`) do not have a base; it doesn't mean anything to talk about whether they are decimal or binary. **Text representations** of values can be decimal, binary, or what have you. Base conversions apply to text representations, not to numeric values. – Pete Becker Feb 10 '22 at 19:40
  • 1
    Oh, and please don't use the floating-point `pow` function when dealing with powers of two, bit-shifting would be enough. Generally speaking you seem to overthink it a little too much. Try using pen and paper to help you figure out how to do it. – Some programmer dude Feb 10 '22 at 19:40
  • 1
    Guys sorry I meant Binary to decimal – CreedCoding Feb 10 '22 at 19:43
  • This code converts a string holding binary digits to an `unsigned long`. It's not "Decimal to binary" (edit: or "binary to decimal"). "Decimal" means "base 10", and there is no code here that deals with _base 10_ or _powers of 10_ or _decimals_. – Drew Dormann Feb 10 '22 at 19:43
  • `std::to_string(this->binary_string[i]);` doesn't do what you think it does. It's converting a `char` to an `int` to a `std::string` representation of that `int`. – Drew Dormann Feb 10 '22 at 19:58
  • `for(int i = this->binary_string.length() - 1; i > 0; i--)` doesn't do what you think it does. It will loop once for a two-character string. It will not loop at all for a one-character string. – Drew Dormann Feb 10 '22 at 20:00
  • Quite a few bugs in this code. `"10"` produces `48` because your code ignores the `'1'` and stores the ASCII value of the `'0'`. It is time to learn to [use a debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). It will be a great help to you! – Drew Dormann Feb 10 '22 at 20:01
  • Off-Topic: Try an experiment: remove all instances of "this->" and see what happens. The `this->` syntax is only needed to differentiate between parameters and members. – Thomas Matthews Feb 10 '22 at 21:03

2 Answers2

0

There is another simpler implementation by strtoull() function for your problem.

More details can be found at C++: binary std::string to decimal

Boris Hu
  • 202
  • 1
  • 6
0

Check this:

    int operator"" _binary(const char *s, size_t z)
    {
        int dec = 0;
        for (int i = 0; i < z; i++)
        {

            dec = dec << z;
            if (s[i] == '1')
            {
                dec += z;
            }
            return dec;
        }
    }