# Converting Binary to Decimal (FORTRAN 95)

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.
###### Who is Participating?

Commented:
I was offline for some time. Had no ideea that you didn't solve your problem yet.
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^9=512 with "-" in front):

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
0

Commented:
If you have the following 10 digits binary number:
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*2^4+1*2^5+1*2^6+0*2^7+0*2^8+1*2^9
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
0

Author Commented:
Thank you for taking the time to respond Wesley.  My main problem is the program.  At the beginning of my program I define the entered binary number as an integer like this     CHARACTER(len=10) :: binary_number
WRITE (*,*) ' Enter 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?
0

Commented:
0

Commented:
Rickfriesen,

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
0

Author Commented:
That link, though it should be helpful, is still a little confusing to me.  The only thing I think I got out of it, though I haven't tried it yet, was:  character(len=20)  :: string
real :: value
string = '1.23e4'
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.
0

Author Commented:
This is what I want to know.  If you were going to write a very simple Fortran90 program to convert Binary to Decimal, how would you do it?

thanks
0

Author Commented:
I would still love some futher assistance with my problem.  It would be greatly appreciated.
0
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.