This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

On the numerical base and conversion between bases

How can C++ code convert different bases such as decimal and octal

Recursive method

#include <iostream> 
using namespace std;
int sb(int n) 
{
    if(n<8)return n;
    else 
	return n%8+10* (sb(n/8));// The idea of recursion is about base conversion
}
int main(a) 
{
    int n;
    cin>>n;    // Get a decimal number
    cout<<sb(n)<<endl;   // Use recursive functions

    return 0;
} 
Copy the code

Circulation method

#include<iostream>
using namespace std;
long long sum=0,b=1,x=1;
int a;
int main(a)
{
    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;
} 
Copy the code

10 to hexadecimal

10turn16Into the system#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(a){
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(intj=i; j>=0; j--) {cout<<a[j]; }cout<<endl;return 0; }Copy the code

Why are there different bases

The base must have been created for better expression or record.

In our life, when we express a number, 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. As we all know, there are other bases in our lives. For example, minutes and seconds of time are in base 60, and angles are in base 360. In computers, binary is used because computers can only represent two states, 0, 1 (0 is like the switch off, 1 is like the switch on). So computers can only “know” binary numbers. Binary “every two carries one”. You may also know octal, hexadecimal, why binary and other bases? If you think about it, humans use the decimal system and computers use binary, but if you want to understand how a computer works, it’s very difficult to express and describe it in binary. Because binary is too long, like an integer (decimal), 32-bit machines have 32 bits in binary. If you use hexadecimal, you only need 8 bits, so it’s much easier to express and describe, and you can also translate each other directly, which is not as complicated as decimal. (because 8,16, is 2 to the third, fourth power.)

The representation method and conversion operation of different bases

BIN Binary OCT octal DEC Decimal HEX Hexadecimal

Convert from decimal to binary using short division:

Binary to decimal bit weight expansion method:

Binary to Octal: Converts 3 bits to 1 bit

Octal to binary: change 1 bit to 3 bits

Binary to hexadecimal: 4 bits to 1 bit

Hexadecimal to binary: 1 bit to 4 bits

Take chestnuts for example