Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert binary string to an NSInteger

Tags:

objective-c

Anybody has some code in Objective-C to convert a binary string to an NSInteger ?

example:

111000 -> 56
like image 934
vincent Avatar asked Dec 17 '22 14:12

vincent


1 Answers

const char* utf8String = [binaryString UTF8String];
const char* endPtr = NULL;
long int foo = strtol(utf8String, &endPtr, 2);

if (endPtr != utf8String + strlen(utf8String))
{
    // string wasn't entirely a binary number
}
if (errno == ERANGE && (foo == LONG_MAX || foo == LONG_MIN))
{
    // number was too big or too small
}
like image 125
JeremyP Avatar answered Jan 02 '23 06:01

JeremyP