Solved

Converting Binary to Decimal (FORTRAN 95)

Posted on 2004-09-22
8
3,317 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now