convert 0 1 string into decimal code example

Example 1: Java convert hex to decimal

public class ConvertHexToDecimal
{
   public static void main(String[] args)
   {
      String strHex = "b";
      int decimal = Integer.parseInt(strHex, 16);
      System.out.println("Decimal number : " + decimal);
   }
}

Example 2: english to decimal

#include<iostream>
#include<string>
#include<limits>

inline std::string binary_conv(int,int,std::string);

int main()
{
    long int number;
    int base {27};
    std::cout<<"\nPlease enter a non-negative integer:\t";
    std::cin>>number;


    while(1)
    {
        if(std::cin.fail())
        {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        std::cout<<"\nYou have entered wrong input!"<<std::endl;
        std::cout<<"\nPlease enter a non-negative integer:\t";
        std::cin>>number;
    }
        if(!std::cin.fail())
        break;
    }
    
    std::string binary_number;
    binary_number = binary_conv(number,base,binary_number);
    
    
    std::cout<<binary_number<<std::endl;
   
    return 0 ;
}

std::string binary_conv(int number, int base,std::string binary)
{   
    std::string zero_one = " abcdefghijklmnopqrstuvwxyz";
    if(number==0)
        return binary;
    binary = zero_one[number%base]+binary;
    number/=base;
    return binary_conv(number,base,binary);
}

Tags:

Java Example