zlib库官网解压缩提供的"八进位数字转long数字"实现:
/* convert octal digits to long */
/* on error return -1 */
long Ungzip_GetOct(char *p, int width)
{
long result = 0;
char c;
while (width--) {
c = *p++;
if (c == 0) { /* ascii码 0x00 (null) 空字符 */
break;
}
if (c == ' ') { /* ascii码 0x20 空格 */
continue;
}
if (c < '0' || c > '7') { /* ascii码 0x30 = '0',0x37 = '7' */
return -1;
}
result = result * 8 + (c - '0');
}
return result;
}
/* 测试用例 */
假设数组test_size存储某个测试文件test.png的大小为141字节.
char test_size[12] = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x32, 0x31, 0x35, 0x20 };
long Result = Ungzip_GetOct(test_size, 12);
最终Result = 141.
(PS: 之所以设置函数返回long 型,是因为文件特别大的时候,int型存储不下.)