开发者

Convert binary string to an NSInteger

开发者 https://www.devze.com 2023-03-06 13:01 出处:网络
Anybody has some code in Objective-C to convert a binary string to an NS开发者_JAVA百科Integer ?

Anybody has some code in Objective-C to convert a binary string to an NS开发者_JAVA百科Integer ?

example:

111000 -> 56


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消