Solved

Converting Binary to Decimal (FORTRAN 95)

Posted on 2004-09-22
8
3,392 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 52
groupNoAdj 7 86
Currency Conversion? 1 73
Adding Namespace to the second node of the xml 16 65
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

895 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

14 Experts available now in Live!

Get 1:1 Help Now