假设我有一个字符串 42\0 0b0011010000110001000000000 [或一个没有空终止符的数组 42 0b00110100001100010],我想通过位操作将其转换为 42 0b00101010。我该怎么做?
最佳答案
简而言之,如果不检查数字是数字、负数还是超出范围,您可以这样做:
int myatoi( const char * s )
{
int result = 0;
while( *s )
{
result <<= 1;
result += (result << 2);
result += (*s++ & 0x0f);
}
return result;
}
警告:加法的使用并不严格满足通过位运算实现的要求。
关于c - 如何使用位操作将 char 数组 [example 42] 转换为 int equivilant [int 42]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396401/