Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integer to char 1

Status
Not open for further replies.

ChristianPrins

Technical User
Feb 7, 2001
1
NL
Is there a way to convert a integer to a character
And if there is a way what is that way

Thanks

Christian
 
A simple way may be:
define a char (or array of char) and an Integer.
put it into an equivalence Statement. (watch the size of the variables)

Probably there is a function which will do it, but my "Fortran" - experience is about 10 years ago.


hnd
hasso55@yahoo.com

 
Unfortunely it isn't working
By assinging the integer value to character I get the ASCII code
So the Result of the program is a 'A'
Program test
integer a
character(10) c
a=65
c=a
write(*,*) c !Result is 'A'
end

But thanks anyway
 
************************************************************************
*
* Routine name: ITOS
************************************************************************
*
* Description:
*
* THIS FUNCTION TRANSLATE INTEGER NUMBER TO STRING
*
* Argument:
*
* NUM INTEGER*4 IN - THE NUMBER TO TRANSLATE
* LENGTH INTEGER*4 IN - THE EXPECTED LENGTH OF THE TRANSLATED
* STRING
*
* return status:
*
* CHARACTER*(*) - THE TRANSLATED STRING
*
************************************************************************

CHARACTER*(*) FUNCTION ITOS(NUM,LENGTH,BLANKS)
IMPLICIT NONE

******************** D E F I N I T I O N S ******************

integer*4 num,
1 length

logical blanks

integer*4 i,
1 int

character*80 str

logical put_zero

intrinsic mod,
1 char,
1 ichar

******************** P R O C E D U R E ******************

put_zero = .true.
if (%loc(blanks) .ne. 0) put_zero = .not. blanks

str = ' '

int = num
do i=0,length-1
str(length-i:length-i) = char(mod(int,10)+ichar('0'))
int = int / 10
if ((.not. put_zero) .and. (int .eq. 0)) goto 10
enddo

10 continue

if (str .eq. ' ') str(length:length) = '0'

itos = str(1:length)

END

 
C A SIMPLER WAY ....
C EXAMPLE
CHARACTER * 10 A
I = 1234
WRITE(A,'(I4)') I
C CHECK :
WRITE(*,'(A4)') A
END
 
I think Mr Anderson presents a classic implementation here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top