Reputation: 385
For example:
string binaryValue = "11111111111111111111111111111011" // -5
I need to convert this string to decimal representatin of this number.
stoi(binaryValue, nullptr, 2)
Will throw exception on this case. So how can i do this in c++ ? String or int doesn't matter.
Upvotes: 2
Views: 3218
Reputation: 2618
as you probably know number are stored as Twos Complement
to convert it using simple pseudocode
flip numbers 0->1, 1->0 from left to write util you find last 1 in string don't toggle this one
this will be your answer 0000000000000000000000000101=5
here is the code brouht from https://www.geeksforgeeks.org/efficient-method-2s-complement-binary-string/
#include<bits/stdc++.h>
using namespace std;
string findTwoscomplement(string str)
{
int n = str.length();
// Traverse the string to get first '1' from
// the last of string
int i;
for (i = n ; i >= 0 ; i--)
if (str[i] == '1')
break;
// If there exists no '1' concat 1 at the
// starting of string
if (i == 0)
return '1' + str;
// Continue traversal after the position of
// first '1'
for (int k = i-1 ; k >= 0; k--)
{
//Just flip the values
if (str[k] == '1')
str[k] = '0';
else
str[k] = '1';
}
// return the modified string
return str;;
}
int main()
{
string str = "11111111111111111111111111111011";
cout << findTwoscomplement(str);
//now you convert it to decimal if you want
cout<<"Hello World";
cout << stoul( findTwoscomplement(str),nullptr,2);
return 0;
}
preview at https://onlinegdb.com/SyFYLVtdf
Upvotes: 0
Reputation: 61202
See the documentation of:
int std::stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
in particular:
The valid integer value [of
str
] consists of the following parts:
- (optional) plus or minus sign
...
...
If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by unary minus in the result type.
Exceptions
std::invalid_argument
if no conversion could be performed
std::out_of_range
if the converted value would fall out of the range of the result type...
In the absence of a preceding minus-sign, the string:
std::string binaryValue = "11111111111111111111111111111011";
will be interpreted in a the call:
std::stoi(binaryValue, nullptr, 2);
as a non-negative integer value in base-2 representation. But as such,
it is out of range, so std::out_of_range
is thrown:
To represent -5 as a string that your std::stoi
call will convert as you expect,
use:
std::string const binaryValue = "-101";
If you don't want to prefix a minus sign to a non-negative base-2 numeral, or cannot do so in your real-world
situation, but wish to interpret "11111111111111111111111111111011"
as the two's complement representation of a signed integer using the std::sto*
API,
then you must first convert the string to an unsigned integer of a wide-enough
type, and then convert that unsigned value to a signed one. E.g.
#include <string>
#include <iostream>
int main()
{
auto ul = std::stoul("11111111111111111111111111111011",nullptr,2);
std::cout << ul << std::endl;
int i = ul;
std::cout << i << std::endl;
return 0;
}
Upvotes: 3