Last active
August 29, 2015 14:17
-
-
Save x5lcfd/13a372d65a87697d8479 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
字符'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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
真正的问题并没有这么简单。