BASE CONVERTER FORTRAN

FORTRAN BASE CONVERTER

Page 1 of 1

9 Replies - 5932 Views - Last Post: 26 October 2011 - 11:27 AM Rate Topic: -----

#1 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

BASE CONVERTER FORTRAN

Posted 10 November 2010 - 07:08 AM

      PROGRAM CONVERTOR
      CHARACTER TEXT*50,SV*25,CONVTD(100)*1,CONVTD2(100)*1
      INTEGER POWDET,FRPOWDET,FRACT(100),WHOLE(100),K,DB,J,INTEG(100),T
     *PREC,NEXT
      DOUBLE PRECISION TOTCONV,P,SB,TOTCONVA,TOTCONVB,CONVTDA(100),C,LEFTA,PROD
7     PRINT*,'ENTER SOURCE BASE'
      READ(*,*)SB
      PRINT*,'ENTER DESTINATION BASE'
      READ(*,*)DB
45    PRINT*,'ENTER SOURCE VARIABLE'
      READ(*,1)SV
1     FORMAT(A25)
      DO 13 I=1,25
      IF(ICHAR(SV(I:I)).GE.97.AND.ICHAR(SV(I:I)).LE.122)THEN
      SV(I:I)=CHAR(ICHAR(SV(I:I))-32)
      ENDIF
13    CONTINUE
      DO 8 I=1,25
      IF(0.LE.SB.AND.9.GE.SB)THEN
      IF(ICHAR(SV(I:I)).GE.SB+48)THEN
      PRINT*,'SUCH A CONVERSION IS IMPOSSIBLE,RE-ENTER THE SOURCE VALUE
     *KEEPING IN MIND THAT  EACH DIGIT SHOULD BE LESS THAN',
     *INT(SB)
      GOTO 45
      ENDIF
      ENDIF
      IF(10.LE.SB)THEN
      IF(ICHAR(SV(I:I)).GE.SB+55)THEN
      PRINT*,'SUCH A CONVERSION IS IMPOSSIBLE,RE-ENTER THE SOURCE VALUE
     *KEEPING IN MIND THAT  EACH DIGIT SHOULD BE LESS THAN',
     *INT(SB)
      GOTO 45
      ENDIF
      ENDIF
8     CONTINUE
      WRITE(*,*)'PRECISION TO HOW MANY DIGITS AFTER COMMA?'
      READ(*,*)PREC
      PRINT*,SV,' IN BASE ',INT(SB),' WITH',INT(PREC),' DIGIT(S) AFTER
     *COMMA (IN BASE ',INT(DB),') ','IS EQUIVALENT TO:'
      C=0
      K=0
      TOTCONV=0
      TOTCONVA=0
      TOTCONVB=0
      FRPOWDET=0
      POWDET=INDEX(SV,'.')-1
      DO 2 I=1,POWDET
      IF(ICHAR(SV(I:I)).GE.48.AND.ICHAR(SV(I:I)).LE.57)THEN
      WHOLE(I)=ICHAR(SV(I:I))-48
      ENDIF
      IF(ICHAR(SV(I:I)).GE.65.AND.ICHAR(SV(I:I)).LE.90)THEN
      WHOLE(I)=ICHAR(SV(I:I))-55
      ENDIF
      IF(SV(I:I).EQ.'.')GOTO 30
      TOTCONVA=TOTCONVA+WHOLE(I)*(SB**(POWDET-I))
2     CONTINUE
30    DO 3 I=POWDET+2,INDEX(SV,' ')-1
      K=K+1
      IF(ICHAR(SV(I:I)).GE.48.AND.ICHAR(SV(I:I)).LE.57)THEN
      FRACT(I)=ICHAR(SV(I:I))-48
      ENDIF
      IF(ICHAR(SV(I:I)).GE.65.AND.ICHAR(SV(I:I)).LE.90)THEN
      FRACT(I)=ICHAR(SV(I:I))-55
      ENDIF
      P=SB**(-1*K)
      TOTCONVB=TOTCONVB+FRACT(I)*P
3     CONTINUE
      LEFTA=TOTCONVA
      DO 40 I=1,100
      IF(0.LE.LEFTA.AND.LEFTA.LE.DB-1)GOTO 50
      IF(DB**I.LE.LEFTA.AND.LEFTA.LE.(DB**(I+1))-1)GOTO 50
40    CONTINUE
50    DO 100 J=I+1,1,-1
      CONVTDA(J)=MOD(LEFTA,DB)
      LEFTA=(LEFTA-MOD(LEFTA,DB))/DB
      IF(CONVTDA(J).GE.0.AND.CONVTDA(J).LE.9)THEN
      CONVTD(J)=CHAR(INT(CONVTDA(J)+48))
      ENDIF
      IF(CONVTDA(J).GE.10.AND.CONVTDA(J).LE.35)THEN
      CONVTD(J)=CHAR(INT(CONVTDA(J)+55))
      ENDIF
100   CONTINUE
      DO 150 J=1,I+1
      TEXT(J:J)=CONVTD(J)
150   CONTINUE
      NEXT=I+1
      TEXT(NEXT+1:NEXT+1)='.'
      NEXT=NEXT+1
      T=0
      DO 200 J=1,PREC+1
      PROD=TOTCONVB*DB
      INTEG(J)=INT(PROD)
      T=T+1
      PROD=TOTCONVB*DB
      IF(INTEG(J).GE.0.AND.INTEG(J).LE.9)THEN
      CONVTD2(J)=CHAR(INT(INTEG(J)+48))
      ENDIF
      IF(INTEG(J).GE.10.AND.INTEG(J).LE.35)THEN
      CONVTD2(J)=CHAR(INT(INTEG(J)+55))
      ENDIF
      IF(TOTCONVB-INTEG(J).EQ.0)GOTO 300
      TOTCONVB=DB*TOTCONVB-INTEG(J)
200   CONTINUE
300   DO 400 J=1,T-1
      TEXT(NEXT+J:NEXT+J)=CONVTD2(J)
400   CONTINUE
      DO 500 I=NEXT+J,50
      TEXT(I:I)=' '
500   CONTINUE
      WRITE(*,*)TEXT
      WRITE(*,*)'DO YOU WANT ANOTHER CONVERSION?'
      READ(*,'(A1)')TEXT(1:1)
      IF(TEXT(1:1).EQ.'Y')GOTO 7
      STOP
      END





Is This A Good Question/Topic? 1
  • +

Replies To: BASE CONVERTER FORTRAN

#2 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: BASE CONVERTER FORTRAN

Posted 10 November 2010 - 07:18 AM

Remember to convert a number, you need to input it in the form XXX.YYY

That is a whole part " dot " fractional part

You can use ABC....Z as bases.

Example : 1.1 in base 2 = 1.5 in base ten

This post has been edited by burakaltr: 10 November 2010 - 07:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: BASE CONVERTER FORTRAN

Posted 10 November 2010 - 12:27 PM

Is this just a sample program, or did you have a question/problem with this code?
Was This Post Helpful? 0
  • +
  • -

#4 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: BASE CONVERTER FORTRAN

Posted 10 November 2010 - 03:46 PM

View PostJackOfAllTrades, on 10 November 2010 - 11:27 AM, said:

Is this just a sample program, or did you have a question/problem with this code?


A Sample one.
Was This Post Helpful? 0
  • +
  • -

#5 stallone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-November 10

Re: BASE CONVERTER FORTRAN

Posted 06 December 2010 - 09:17 PM

I do not intend to violate the forum rules. But this program ranks #1 in google search :

http://www.google.co...b48c5b65836e6db

Anyone find it useful ? Please write here your experiences. Maybe some of you have used it in your HW or anything.

As I have said I do not want to violate forum rules.
Was This Post Helpful? 0
  • +
  • -

#6 phemzy1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 11

Re: BASE CONVERTER FORTRAN

Posted 26 October 2011 - 05:38 AM

@bulkaltr

Is there any inbuilt function to convert from base 2 to base 10 (decimal) like EPBASE
Was This Post Helpful? 0
  • +
  • -

#7 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: BASE CONVERTER FORTRAN

Posted 26 October 2011 - 05:48 AM

View Postphemzy1, on 26 October 2011 - 05:38 AM, said:

@bulkaltr

Is there any inbuilt function to convert from base 2 to base 10 (decimal) like EPBASE


Well, to tell the Truth. I used WF77 so I Really am not Sure if there's one.

Have a Beautiful Day !
Was This Post Helpful? 0
  • +
  • -

#8 phemzy1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 11

Re: BASE CONVERTER FORTRAN

Posted 26 October 2011 - 05:54 AM

@burakaltr

I'm trying to convert only from base 2 to base 10 and i don't know how to go about it.
Was This Post Helpful? 0
  • +
  • -

#9 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: BASE CONVERTER FORTRAN

Posted 26 October 2011 - 05:56 AM

View Postphemzy1, on 26 October 2011 - 05:54 AM, said:

@burakaltr

I'm trying to convert only from base 2 to base 10 and i don't know how to go about it.


It won't be hard, pal.

Obtain all digits by the method in my program and then multiply by respective powers of 2.
Was This Post Helpful? 0
  • +
  • -

#10 phemzy1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 11

Re: BASE CONVERTER FORTRAN

Posted 26 October 2011 - 11:27 AM

@burakaltr

The ICHAR convert character to integer. I want the character value to be inputed.

i.e.
integer i
i=ichar('a')
print*,a

the return value is 97 which is equal to a on the ascii table. but what i want is to allow the value of a to be inputed from the keyboard

i.e.
integer i, A
i=ichar('A')
print*,i

return: if the value of A given is 5 the expected result should be 53 but it won't work like that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1