有人在 Objective-C 中有一些代码可以将二进制字符串转换为 NSInteger 吗?

例子:

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
}

关于objective-c - 将二进制字符串转换为 NSInteger,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6042029/

10-09 01:33