Skip to content

Instantly share code, notes, and snippets.

@x5lcfd
Last active August 29, 2015 14:17
Show Gist options
  • Save x5lcfd/13a372d65a87697d8479 to your computer and use it in GitHub Desktop.
Save x5lcfd/13a372d65a87697d8479 to your computer and use it in GitHub Desktop.
/*
字符'0'在机器中存放的是其ASCII码,‘0‘对应的是0x30,以二进制表示“00110000”。
从’0’-‘9’对应的ASCII码为0x30-0x39,并且在数值上,分别对应后四位二进制所表示
的数值,如‘1’->0x31->00110001,后四位为0001。所以可以使用移位来得到后四位,
然后根据十进制的性质,每次×10逐步得到结果。
*/
long c2i (char str[]) {
long b = 0;
while(*str != '\0') {
b = b*10 + ((unsigned char)((char)(*(str++)<<4))>>4);
}
return b;
}
@x5lcfd
Copy link
Author

x5lcfd commented Jul 16, 2015

真正的问题并没有这么简单。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment