Created
November 19, 2017 02:19
-
-
Save igaozp/c63f851da797f7a7c22030b8a06f76ee 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
| #include <iostream> | |
| #include <cmath> | |
| using namespace std; | |
| void hex_to_dec(string &hex) { | |
| long long dec = 0; | |
| for (int i = hex.length() - 1; i >= 0; i--) { | |
| int num = 0; | |
| if (hex[i] >= '0' && hex[i] <= '9') { | |
| num = hex[i] - 48; | |
| } else { | |
| num = hex[i] - 55; | |
| } | |
| dec += num * pow(16, hex.length() - i - 1); | |
| } | |
| cout << dec << endl; | |
| } | |
| int main() { | |
| string s; | |
| cin >> s; | |
| hex_to_dec(s); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment