Created
November 19, 2017 02:41
-
-
Save igaozp/2f2c4bdb5c0d98b0ccea817366edc6a9 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 <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