Skip to content

Instantly share code, notes, and snippets.

@igaozp
Created November 19, 2017 02:19
Show Gist options
  • Select an option

  • Save igaozp/c63f851da797f7a7c22030b8a06f76ee to your computer and use it in GitHub Desktop.

Select an option

Save igaozp/c63f851da797f7a7c22030b8a06f76ee to your computer and use it in GitHub Desktop.
十六进制转十进制
#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