C++ decimal to hexadecimal conversion

Author of the article: Caspian
Source website: Caspian C\C++ column


Decimal to hexadecimal conversion

#include <iostream>  
#include <string>  
using namespace std;

//十进制整数转十六进制字符串
string DecimalToHex(long long decimal)
{
    
    
    string hex = "";
    while (decimal > 0) 
    {
    
    
        int remainder = decimal % 16;
        if (remainder < 10) 
            hex = (char)(remainder + '0') + hex;
        else 
            hex = (char)(remainder + 'A' - 10) + hex;
        decimal /= 16;
    }
    return hex;
}

//十六进制字符串转十进制整数
long long HexToDecimal(string hex)
{
    
    
    long long decimal = 0;
    for (int i = 0; i < hex.size(); i++)
    {
    
    
        char c = hex[i];
        if (c >= '0' && c <= '9')
            decimal = decimal * 16 + (c - '0');
        else
            decimal = decimal * 16 + (c - 'A' + 10);
    }
    return decimal;
}

int main() 
{
    
    
    long long n = 3123197280;
    cout << n << endl;

    string hex = DecimalToHex(n);
    cout << hex << endl;

    n = HexToDecimal(hex);
    cout << n << endl;

    cin.get();
    return 0;
}

Output:
3123197280
BA283560
3123197280

Insert image description here

Base conversion

Conversion between decimal and hexadecimal is very important in computer science and in daily life. Decimal is a counting system commonly used in our daily life, counting based on 10 different numbers (0-9). Hexadecimal is a counting system used internally by computers, based on 16 different numbers (0-9 and AF).

To do decimal to hexadecimal conversion we can use the following steps:

  • Split a decimal number into two parts to the left and right of the decimal point.
  • Convert the decimal part to hexadecimal, each hexadecimal number corresponds to a 4-digit binary number. For example, the binary representation of 0.375 is 0.011, which then converted to hexadecimal is 0.3.
  • Convert the integer part to hexadecimal in the same way. For example, the binary representation of 12 is 1100, which when converted to hexadecimal is C.
  • Combine the results of the decimal and integer parts to get the final hexadecimal representation. For example, combining 0.3 and 12, the resulting hexadecimal representation is 0.3C.

Likewise, to convert from hexadecimal to decimal, we can use the following steps:

  • Split a hexadecimal number into two parts to the left and right of the decimal point.
  • Convert the decimal part to binary, and each hexadecimal number corresponds to a 4-digit binary number. For example, the binary representation of 0.3 is 0.0111, which then converted to decimal is 0.375.
  • Convert the integer part to binary in the same way. For example, the binary representation of C is 1100, and then converted to decimal it is 12.
  • Combine the results of the decimal and integer parts to get the final decimal representation. For example, combining 0.375 and 12, the resulting decimal representation is 12.375.
    In computer science, the use of hexadecimal is very common because it can effectively represent binary data, and the way binary processes data internally in computers maps very naturally to hexadecimal representation.

Guess you like

Origin blog.csdn.net/WangPaiFeiXingYuan/article/details/134500608