Converting a string of 1's & 0's to an int

kherring79
kherring79 used Ask the Experts™
on
I have a string containing 25 1's & zeros and i would like to convert this into an integer.

compiler is gcc on redhat 7.4.


thanks

kevin
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
actually dont worry, i have figured it out myself!
Most Valuable Expert 2014
Top Expert 2015
Commented:
#include <stdlib.h>
int i=strtol(string,0,2);

Author

Commented:
sorry, i meant if i have a string containing 101010 i want to return the integer 42, but it doesnt matter now, i have written a little function that does it.

thanks

kevin
kherring,
The one-liner that ozo gave you will do exactly what you want. :-)
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to ozo

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial