Search
 
SCRIPT & CODE EXAMPLE
 

CPP

binary to decimal

double binaryToDecimal(char binary[DIM]) {
	char binary2[DIM] = "", floa[DIM] = "";
	double decimal = 0, negDec = 0, flo = 0;
	int count = 0, j = 0, i = 0, f = 0, g = 0, h = 0, count1 = 0, d = 0, k = 0;
	while (binary[d] != ''&&binary[d] != '.') { d++; }
	d--;
	if (binary[0] == '-') {
		while (binary[k] != '') {
			binary[k] = binary[k + 1];
			k++;
		}
		k = 0;
		while (binary[k] == '0') {
			d--;
			k++;
		}
		negDec = pot(2.000, d*1.000, 1);
	}
	while (binary[i] != ''&&binary[i] != '.') {
		i++;
	}
	i--;
	count = i;
	h = i;
	while (binary[h] != '') {
		floa[g] = binary[h + 2];
		g++;
		h++;
	}
	g--;
	count1 = g;
	while (i >= 0) {
		binary2[j] = binary[i];
		i--;
		j++;
	}
	binary2[j] = '';
	while (i <= count) {
		if (binary2[i] == '1') {
			decimal = decimal + pot(2.000, i, 1);
		}
		i++;
	}
	h = -1;
	g = 0;
	while (g <= count1) {
		if (floa[g] == '1') {
			flo = flo + pot(2.000, h, 1);
		}
		g++;
		h--;
	}
	decimal = decimal + flo;
	if (negDec > 0) {
		decimal = (decimal - negDec);
		if (flo > 0) {
			double f = fl(decimal);
			f = 1 - f;
			decimal = returnDeciPart(decimal) - f;
		}
	}
	return decimal;
}
Comment

binary to decimal

"""Any base to decimal
>>> CHECK THE SOURCE! <<<

Conversion of number from any base to decimal!

Answer by paw88789 on Nov 2, 2017 on Math StackExchange:
+------------------------------------------------------------------------+
  A similar thing occurs if someone reads you, digit-by-digit,...
  ...left-to-right, a base ten numeral:
  E.g. The actual numeral is 3702, but you only get one digit at a time:

  [3] Current value is 3.
  [7] Current the value is 3⋅10+7=37.
  [0] Current the value is 37⋅10+0=370.
  [2] Current the value is 370×10+2=3702.

  End of number. The last current value is the actual value.

  At each step, you multiply the previous total by the base...
  ...and add the next digit.
+------------------------------------------------------------------------+
"""
Comment

binary to decimal online converter

//* How to convert a decimal number into a binary number
#include <bits/stdc++.h>
using namespace std;

class NumberConversion
{
    int n = 0, count = 0, ans = 0;

public:
    void decimalToBinary(int n)
    {
        this->n = n;
        while (this->n != 0)
        {
            int rem = this->n % 2;
            this->n /= 2;
            ans += rem * pow(10, count);
            count++;
        }
    }

    void display()
    {
        cout << ans << endl;
    }
};

int main()
{
    int number = 0;
    cin >> number;
    NumberConversion num1;
    num1.decimalToBinary(number);
    num1.display();

    return 0;
}
Comment

Binary number to Decimal number

#include <stdio.h>
int main()
{
      int  num, binary_val, decimal_val = 0, base = 1, rem;
      printf("Insert a binary num (1s and 0s) 
");
      scanf("%d", &num); 
      binary_val = num;
      while (num > 0)
      {
          rem = num % 10;
          decimal_val = decimal_val + rem * base;
          num = num / 10 ;  //these are the correct lines
          base = base * 2;  //these are the correct lines
      }
      printf("%d 
", binary_val);
      printf("%d 
", decimal_val);
   return 0;
}
Comment

binary to decimal

import java.util.Scanner;

public class BinaryToDecimal {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String binary = sc.next();
		// This is one line solution for binary to decimal in java
		System.out.println(Integer.parseInt(binary,2));
		
		//This solution is based on formula for binary to decimal conversion
		int n=0,dec=0;
		for(int i=binary.length()-1;i>=0;i--)
		{
			dec = dec + Integer.parseInt(String.valueOf(binary.charAt(i)))*(int)Math.pow(2,n);
			n++;
		}
		System.out.println(dec);

	}

}
Comment

Convert Binary to Decimal using int()

#Python Interactive Shell
>>> int('1001011', 2)
75
Comment

PREVIOUS NEXT
Code Example
Cpp :: sort an array in c++ 
Cpp :: How to see gateway on linux 
Cpp :: c++ function overloading 
Cpp :: C++ switch..case Statement 
Cpp :: c++ itoa 
Cpp :: replace a char in string c++ at a specific index 
Cpp :: c++ constructor 
Cpp :: cpp compare strings 
Cpp :: pragma HLS bracets 
Cpp :: cpprestsdk send file 
Cpp :: qt c++ qdockwidget remove title 
Cpp :: enter items in array until enter is pressed c++ 
Cpp :: heapsort 
Cpp :: the amount of input is unknown 
Cpp :: subtraction of a 2d matrix in c++ 
Cpp :: warning: base will be initialized after 
Cpp :: find n unique integers sum up to zero 
Cpp :: initalising array c++ 
Cpp :: sfml thread multi argument function 
Cpp :: check .h files syntax c++ 
Cpp :: linq select where string equals "String" 
Cpp :: Link List Insertion a node 
Cpp :: cout two dimension array c++ 
Cpp :: stack algorithm in c++ 
Cpp :: get shape of eigen matrix 
Cpp :: sort vector in c 
Cpp :: c++ merging algorithm 
Cpp :: C++ Rectangular Form 
Cpp :: Snake Procession codechef solution in c++ 
Cpp :: stp 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =