About conversion between bases and different bases

About conversion between bases and different bases

How to realize the conversion of different bases in C++ code

Take decimal and octal sum conversion as an example

Recursive method

#include <iostream> 
using namespace std;
int sb(int n) 
{
    
    
    if(n<8)return n;
    else 
	return n%8+10*(sb(n/8));//递归思想关于进制转化 
}
int main() 
{
    
    
    int n;
    cin>>n;    //获取10进制数 
    cout<<sb(n)<<endl;   //利用递归函数 

    return 0;
} 

Loop method

#include<iostream>
using namespace std;
long long sum=0,b=1,x=1;
int a;
int main()
{
    
    
    cin>>a;
    while(a!=0)
    {
    
    
        sum=sum+(a%8)*x;
        a/=8;   //a=a/8
        x*=10;  //x=x/10
    }
    cout<<sum<<endl;
   
    return 0;
} 

10 to hexadecimal

1016进制
#include<iostream>using namespace std;
char exchange(int n)
{
    
         switch(n)
{
    
         case 0:    return '0';break;
  case 1:    return '1';break;
case 2:    return '2';break;
          case 3:    return '3';break;
          case 4:    return '4';break;
          case 5:    return '5';break;
case 6:    return '6';break;
case 7:    return '7';break;
           case 8:   return '8';break;
      case 9:  return '9';break;
      case 10: return 'A';break;
case 11:return 'B';break;
      case 12:return 'C';break;
      case 13:return 'D';break;
      case 14: return 'E';break;
      case 15:return 'F';break;}}
int main(){
    
    
int m,note,i=0,s=0;
    cin>>m;
    note=m;
    while(m/16!=0)
    {
    
     i++;  m=m/16;  }
char a[i];
 while(note/16!=0)
    {
    
      a[s]=exchange(note%16);
        s++;
        note=note/16; }
    a[i]=exchange(note);
    for(int j=i;j>=0;j--)
    {
    
    cout<<a[j];
    }cout<<endl; return 0;}

There will be different bases for gods and demons

The generation of the base must be for better expression or record.

When expressing a certain number in our lives, such as the number of grades, such numbers are all decimal numbers. There are 10 decimal numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Everyone knows the decimal system "every decimal one". There are other systems in life. For example, the number of minutes and seconds of time is 60 degrees, and the angle is 360 degrees.
Binary is used in the computer, because the computer can only represent two states 0, 1, (0 is like a switch off, and 1 can be expressed as a switch on). So computers can only "recognize" binary numbers. Binary "every two enters one".
You may also know that there are octal and hexadecimal systems. Why are there binary systems and other systems?
It can be imagined that humans use decimal and computers use binary. However, if you want to better study and understand the operation of computers, using binary expressions and descriptions will be very unintuitive and inconvenient. Because the binary is too long, such as an integer (decimal), the binary in a 32-bit machine has 32 bits. If you use hexadecimal, it only needs 8 digits, which is much more convenient for expression and description. It can also be directly converted when converting between them, which is not as complicated as decimal. (Because 8, 16, is 2 to the 3, 4 power)

Different base representation methods and conversion operations

BIN Binary OCT Octal DEC Decimal HEX Hexadecimal

Decimal to binary uses short division:

Binary to decimal bit weight expansion method:

Binary to octal: convert 3 bits to 1 bit

Octal to binary: change 1 bit to 3 bit

Binary to hexadecimal: 4 bits to 1 bit

Hexadecimal to binary: 1 bit to 4 bits

Give a chestnutBase conversion

Guess you like

Origin blog.csdn.net/qq_51808107/article/details/111028166