This time I will show you how to convert decimal numbers to hexadecimal, and what are the precautions for converting decimal numbers to hexadecimal. The following is a practical case, let's take a look. .
Question
Input a decimal number and output the hexadecimal number corresponding to the decimal number
Idea
First use When a decimal number is divided by 16, the remainder is a number that cannot be carried, so it is written in the lowest digit. The quotient means how many 16s there are. If the quotient is greater than or equal to 16, it means that carry can be continued, then use the quotient to divide Take 16, and write the remainder in the second to last digit... Continue this way until no carry is allowed
Code
#include <iostream>#include<string>using namespace std;string m = "0123456789ABCDEF";int main(){ int n; cin >> n; string ans = ""; while (true) { if (n < 16) { ans = m[n] + ans; break; } int w = n % 16; ans = m[w] + ans; n = n / 16; } cout << ans; }
I believe you have mastered the method after reading these cases, more exciting Please pay attention to php Chinese website
Otherrelated articles! Related reading:
How to determine the baseline of various types of boxes in HTMLWhat are the functions of html semantics What are the tips for using scroll bars in HTMLHow to use the input text box and the img verification codeThe above is the detailed content of How to convert decimal number to hexadecimal. For more information, please follow other related articles on the PHP Chinese website!