Binary to Decimal & Decimal to Binary in C++ / Python Using Some inbuild Functions.

Binary to Decimal & Decimal to Binary in C++ / Python Using Some inbuild Functions.

Table of contents

C++ :

  1. Decimal to Binary

    
     // return binary string with size 'b' which is binary representation of decimal number 'num'
     bitset<b>(num).to_string();
    
     Example : 
    
     // Binary String 't' of 65 with 32 bits 
     string t = bitset<32>(65).to_string();
    
  2. Binary to Decimal

    
     // Example : 
     // Convert 'binarystring' into decimal 
     string binarystring = "0100";
     int num = stoi(binarystring, 0, 2);
    

Python :

  1. Decimal to Binary

     # Convert number 'a' into binary and store into 'a' itself
     a = bin(a)[2:]
    
  2. Binary to Decimal

     # Convert binary string 
     decimale = int(binarystring, 2)
    
     # Example 
     decimal = int("1010", 2)