C ++ conversion of the Hex

First, decimal, octal, hexadecimal between conversion

C language, octal, decimal, hexadecimal can use its corresponding placeholder input or output.

Signed decimal integer:% d,

Unsigned octal integer:% o,

Hexadecimal number 0f unsigned hexadecimal integer:% x,

Hexadecimal number 0f unsigned hexadecimal integer:% X.

 

Examples are as follows:

#include <the iostream> the using namespace STD; int main () 
{ int NUM;
     // read into a decimal number 
    scanf_s ( " % D " , & NUM);
     // output octal 
    the printf ( " % O \ n- " , NUM );
     // output decimal form 
    the printf ( " % D \ n- " , NUM);
     // output hexadecimal form 
    the printf ( " % X \ n- " , NUM); int num2;
     // reads an octal number 
    scanf_s ( " % O "

 


    

    , &num2);
    printf("%o\n", num2);
    printf("%d\n", num2);
    printf("%x\n", num2);

    return 0;
}

 

Second, Decimal to any other hex

#include <the iostream> the using namespace STD; // binary conversion function, is converted into N-ary several few void Trans ( int NUM, int N) {
     IF (NUM> N - . 1 ) { 
        Trans (NUM / N, N ); 
    } 
    COUT << num% N; 
} int main () 
{ int num; 
    CIN >> num;
     // convert binary output num 5 
    Trans (num, 5 ); 
    COUT << endl; return 0 ; 
  }

 





    

         

 

Guess you like

Origin www.cnblogs.com/skyeisgood/p/12511698.html