return 0 in converting string to integer

Hi.
I used atoi(string1.c_str()) for convertig a binary string to integer.
but it always return 0.
why?????!!!!1
What do you mean when saying "binary string"? atoi can only convert to int a representattion of an integer number writtten in decimal notation.
Last edited on
I mean that in this string we just have 0,1.
It must be that the string was not a valid decimal number. What exactly was the string?

By the way, you can use strtol() to convert a string in any base from 2 to 36 into a long int.

http://www.cplusplus.com/reference/cstdlib/strtol/
I wrote like this:
1
2
3
string number;
cin>>number;
int value=atoi(number.c_str())
Yes, but what does the string contain?
If 'number' does not represent an integer literal then of course you will not get a valid value.
it contains 1 and 0.

edit:
it can get a number between and including 0 and 9
Last edited on
May I ask, 1 and 0 like this, "1 0" or " 0 1 " or "10" or what?

it can get a number
what is "it", please?

At any rate, atoi() should be able to handle any string which converts to a valid integer, usually in the range "-2147483648" to "+2147483647". The string "10" certainly fits within that range.

As stated in the reference below, if the input is not a valid integer, the result is zero. http://www.cplusplus.com/reference/cstdlib/atoi/


Last edited on
How about this?
1
2
int value;
cin >> value;
my problem is similar, I have to convert a string to int and strings.

For example, 123TCat and Dog W346 edf fedh are good

assign 123 to x
Cat and dog to str
346 to y
are good to line
usually in the range "-2147483648" to "+2147483647"

ok,it was my problem.
number was out of this range!!
Thank you
Well, that was hard work. I asked three times what was the exact string, and still got no answer. We would have reached the solution much faster if we worked together in cooperation.
ok,I'm sorry!!
Thank you again
Last edited on
Topic archived. No new replies allowed.