forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to convert int into character

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
pban92



Joined: 23 Nov 2009
Posts: 38

PostPosted: Mon Dec 21, 2009 6:27 pm    Post subject: How to convert int into character Reply with quote

How can I convert integer variable, say (integer :: i) into a character variable (character :: i)?

I know that any integer variable (integer :: i) can be converted into real variable (real :: i) by simply doing REAL(i). Is there any similar thing for integer to character variable conversion?

Many thanks,

pban92
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2816
Location: South Pole, Antarctica

PostPosted: Mon Dec 21, 2009 7:03 pm    Post subject: Reply with quote

character*10 charvar
integer integervar

write(charvar,'(i10)') intergervar
Back to top
View user's profile Send private message
cyoung



Joined: 17 Dec 2009
Posts: 1

PostPosted: Mon Dec 21, 2009 10:24 pm    Post subject: Reply with quote

i remmber doing this during my thesis time. I can't memorize but everything is available online. search the code in Google. Smile
_________________
rugby coaching
Back to top
View user's profile Send private message
EKruck



Joined: 09 Jan 2010
Posts: 224
Location: Aalen, Germany

PostPosted: Tue Nov 23, 2010 12:17 pm    Post subject: Reply with quote

Hi pban92,

I have just found your question. Here is a function for your purpose:

!-------------------------------------------------------------------------------
CHARACTER*20 FUNCTION cIntToChar(iVal) ! Convert one integer value to a leftbound character string

! COPYRIGHT: Dr. Erwin Kruck, Aalen, Germany, 2010

IMPLICIT NONE
INTEGER iVal ! Given integer value
INTEGER iNum ! Local iVal (counted down)
INTEGER iMinus ! = ONE if iVal negative; else ZERO
INTEGER iNull ! Characher value of Zero
INTEGER nDigits ! Number of digits (without minus sign)
INTEGER I, N, M

iNum = ABS(iVal)
iNull = ICHAR('0')
cIntToChar = ' '

iMinus = 0
IF (iVal .LT. 0) THEN
iMinus = 1
cIntToChar(1:1) = '-'
ENDIF
nDigits = MAX (1+INT (LOG10 (FLOAT (MAX (1,iNum)))), 1) ! Number of digits

DO I= 1, nDigits
N = nDigits -I +1 +iMinus
M = iNum / 10
cIntToChar(N:N) = CHAR(iNull + iNum - M * 10)
iNum = M
ENDDO

RETURN
END
!-------------------------------------------------------------------------------

A function for a real value can be developed very similar.
Erwin
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group