Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save igaozp/2f2c4bdb5c0d98b0ccea817366edc6a9 to your computer and use it in GitHub Desktop.
十进制转十六进制
#include <iostream>
#include <algorithm>
using namespace std;
void dec_to_hex(long long &dec) {
string hex;
if (dec == 0) {
hex = "0";
}
while (dec != 0) {
long long temp = dec % 16;
if (temp >= 0 && temp <= 9) {
hex += (char) (temp + 48);
} else {
hex += (char) (temp + 55);
}
dec /= 16;
}
reverse(hex.begin(), hex.end());
cout << hex << endl;
}
int main() {
long long num;
cin >> num;
dec_to_hex(num);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment