This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

I'm in a bind. I can't figure out how to convert Binary to Decimal form. I enter in the Binary number as a Character with length=10, because that's what I need. But then if the Binary number is a Character then how do I convert it to an Integer, or do I? Sould I not have entered the Binary number as a Character? I need your help quickly.

thank you.

thank you.

1001101011

write it backwards, like this: 1101011001

then calculate the decimal number like this:

1*2^0+1*2^1+0*2^2+1*2^3+0*

The coefficients of 2^0, 2^1, 2^2, 2^3....2^9, are the digits of your binary number written backwards.

The result of the conversion from binary to decimal in this case is: 619

You can write a little program to take each part of your binary number, perform each multiplication and calculate the sum.

Your question is a little confusing. I hope this is what you wanted to know.

Regards,

Wesley

WRITE (*,*) ' Enter binary number'

READ (*,*) binary_number

I made the binary number a character because I thought that that would be the easiest way to look at each number of the character string to determine if it was a zero or one. But since it's a character, fortran won't let me use =,<,>,etc. like this

IF (binary_number(1:1)=1) THEN

............

I can't compare characters with integers. Was that the wrong way to start out? What should I do?

Following may be of help.

http://www.codecomments.com/message841808.htm

http://www.codecomments.com/message841808.htm

Considering your last comment, I think that ITcrow's link sends you to the right place for solving your problem. Click on it and read the last post on the page.

Wesley

real :: value

string = '1.23e4'

read( string, * ) value

But what is the '1.23E4' for, and what exactly is it supposed to do? I was told by another person that using the MOD function would be easiest, but I don't know how to incorporate that either.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

For transforming a binary number "S" to a decimal number "I" where I is an integer, you input a character of any length (noted as len = *), which is the binary representation.

Here is the code for conversion, which does what I said in my previous comment, considering also the sign (don't know if you need that! - if you don't need, just delete the code for placing the sign); as example, the output for: "-1000000000" will be "-512" (0*2^0+0*2^1+0*2^2+...1*2^

subroutine binary_to_decimal ( s, i )

implicit none

!

character c

integer i

integer ichr

integer isgn

integer istate

integer nchar

character ( len = * ) s

!

nchar = len_trim ( s )

i = 0

ichr = 1

istate = 0

isgn = 1

do while ( ichr <= nchar )

c = s(ichr:ichr)

!

! Blank.

!

if ( c == ' ' ) then

if ( istate == 2 ) then

istate = 3

end if

!

! Sign, + or -.

!

else if ( c == '-' ) then

if ( istate == 0 ) then

istate = 1

isgn = - 1

else

istate = - 1

end if

else if ( c == '+' ) then

if ( istate == 0 ) then

istate = 1

else

istate = - 1

end if

!

! Digit, 0 or 1.

!

else if ( c == '1' ) then

i = 2 * i

i = i + 1

istate = 2

else if ( c == '0' ) then

i = 2 * i

istate = 2

!

! Illegal or unknown sign.

!

else

write ( *, '(a)' ) ' '

write ( *, '(a)' ) 'BINARY_TO_I - Serious error!'

write ( *, '(a)' ) ' Illegal digit = "' // c // '"'

write ( *, '(a)' ) ' Conversion halted prematurely!'

return

end if

if ( istate == -1 ) then

write ( *, '(a)' ) ' '

write ( *, '(a)' ) 'BINARY_TO_I - Serious error!'

write ( *, '(a)' ) ' Unable to decipher input!'

return

end if

if ( istate >= 3 ) then

exit

end if

ichr = ichr + 1

end do

!

! Apply the sign.

!

i = isgn * i

return

end

Regards,

Wesley