Solved

Converting Binary to Decimal (FORTRAN 95)

Posted on 2004-09-22
8
3,433 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:rickfriesen
  • 4
  • 3
8 Comments
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12129124
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 Comment

by:rickfriesen
ID: 12130235
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'
                                                                           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?
0
 
LVL 5

Expert Comment

by:ITcrow
ID: 12130793
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12131020
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 Comment

by:rickfriesen
ID: 12133865
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'
                         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.
0
 

Author Comment

by:rickfriesen
ID: 12134160
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 Comment

by:rickfriesen
ID: 12140007
I would still love some futher assistance with my problem.  It would be greatly appreciated.
0
 
LVL 5

Accepted Solution

by:
WesleySaysHi earned 500 total points
ID: 12140823
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Program to display an alert on Windows Toolbar 2 77
python sqlite question 11 46
This code tracks birthdays 3 73
Full Screen problem when auto scale IFRAME 2 79
This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question