binary to decimal conversion!

30 Mar 2010

Can anybody point me in the direction of some code to perform binary to decimal conversion!?

31 Mar 2010

Could you explain more? Perhaps using some example code so that others can understand the issue better.

31 Mar 2010

Im using this code taken from the mbed forum created by Kim Sec Tee:

 

//ch0.

        cs=0;                               //cs low. start data collection

        _spi.write(0x01);                   //write start bit. Not expecting return

        _data1=_spi.write(0x80);            //write control bit, expecting return B9, B8

        _data2=_spi.write(0x00);            //write anything, expecting return B7-B0

        int ch0 =(_data1<<8) | _data2;      //merge two 8bits data becoming 16bits data

        ch0 = ch0 & 0x03FF;                 // mask off bit15 - bit10

        cs=1;                               //cs high. end of data collection

 

(full code here: http://mbed.org/forum/mbed/topic/534/)

 

So I want to take the value stored in ch0 and change it into a decimal number for manipulation! From what I understand of the code the value stored in ch0 is in binary is this correct?

 

After some more research after posting yesterday I found a method to convert the binary to decimal. You take the first bit and times it by one second bit and times it by two ect. The thing im stuck on with this method is how you separate each of the bits from each other!?

31 Mar 2010

Hello Martin

The data in ch0 is a 10-bit number after the mask operation. You can just treat it as an integer. Every number (real or integer) is stored as binary in computer.

For example, if you want to add 10 to this number, you can simply do:

ch0 += 10;

 

However, if you want to get a 10-based ASCII representation of the binary number, then you can use printf or cout like functions.

31 Mar 2010

Cheers man!

11 Feb 2017

try this...

String str = "100"; double j=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)== '1'){ j=j+ Math.pow(2,str.length()-1-i); } }

Check....full source

http://net-informations.com/java/sts/pro.htm

Helsy