How to convert decimal number to hexadecimal

php中世界最好的语言
Release: 2018-02-26 10:25:42
Original
9385 people have browsed it

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;
}
Copy after login

I believe you have mastered the method after reading these cases, more exciting Please pay attention to php Chinese website

Other

related articles! Related reading:

How to determine the baseline of various types of boxes in HTML


What are the functions of html semantics


What are the tips for using scroll bars in HTML


How to use the input text box and the img verification code

The 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!