View previous topic :: View next topic |
Author |
Message |
Anonymous Guest
|
Posted: Tue Jun 06, 2006 5:04 am Post subject: Integer to Character type casting |
|
|
Hello,
I am attempting to convert a set of integers into their equivalent characters and append these characters to the end of some other text:
mycharvar='text'//char(J)
where J is an integer. When I print this i get a small rectangle (looks like an end of line character) instead of the converted integer I want.
I find this a bit bizarre- can anyone help?
Thanks
DM |
|
Back to top |
|
|
brucebowler Guest
|
Posted: Tue Jun 06, 2006 7:15 am Post subject: Integer to Character type casting |
|
|
char(1) is equivalent to "Ctrl/A", not "1". char(49 [31 hex]) is equivalent to "1" |
|
Back to top |
|
|
Anonymous Guest
|
Posted: Tue Jun 06, 2006 8:30 am Post subject: Integer to Character type casting |
|
|
Thanks for the reply-
In that case, how do I convert
integer e.g. 1
to character e.g. '1'
DM |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7924 Location: Salford, UK
|
Posted: Tue Jun 06, 2006 9:50 am Post subject: Integer to Character type casting |
|
|
Add ICHAR('0') which is 48.
For J in the range 0 to 9 you can use
CHAR(ICHAR('0')+J)
Alternatively you can use an "internal" WRITE statement in which you write to a character variable. |
|
Back to top |
|
|
Anonymous Guest
|
Posted: Wed Jun 07, 2006 4:26 am Post subject: Integer to Character type casting |
|
|
Hello again,
Attempting to use internal write to resolve the problem:
CHARACTER*10 BUSY1,BUSY2
INTEGER VALUE,IO_STAT,T
T=1
WRITE (FMT=*,UNIT=BUSY1,IOSTAT=IO_STAT) T
WRITE (FMT=*,UNIT=BUSY2,IOSTAT=IO_STAT) UPTIME(T)
WRITE(2,*) 'TURBINE'//BUSY1, BUSY2
The output comes out blank after TURBINE is displayed.
Also, I understood that the only necessary parameter for internal write is the 'UNIT=Variable' part - when I remove the other parameters the compiler says:'You may not write to a file that is read-only'
In a similar vein, is it possible to format output of mixed type? |
|
Back to top |
|
|
JohnH
Joined: 26 Sep 2005 Posts: 13
|
Posted: Wed Jun 07, 2006 5:47 am Post subject: Integer to Character type casting |
|
|
After filling in the "missing" bits like the definition of UPTIME and specifying a format for the internal write statements, does the following help you?
PROGRAM INTERNAL_WRITE
CHARACTER*10 BUSY1,BUSY2,UPTIME(2)
INTEGER VALUE,IO_STAT,T
UPTIME(1)=' hello'
T=1
WRITE (FMT='(I10)',UNIT=BUSY1,IOSTAT=IO_STAT) T
WRITE (FMT='(A10)',UNIT=BUSY2,IOSTAT=IO_STAT) UPTIME(T)
WRITE(2,*) 'TURBINE'//BUSY1, BUSY2
STOP
END
which produces this output:-
TURBINE 1 hello
|
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7924 Location: Salford, UK
|
Posted: Wed Jun 07, 2006 5:48 am Post subject: Integer to Character type casting |
|
|
The initial problem is that you need more than 10 characters for the first list-directed output (FMT=*).
Maybe you will need to use formatted output. |
|
Back to top |
|
|
Anonymous Guest
|
Posted: Wed Jun 07, 2006 7:10 am Post subject: Integer to Character type casting |
|
|
Problem solved, thank you so much for your help. |
|
Back to top |
|
|
Anonymous Guest
|
Posted: Fri Jun 09, 2006 3:02 pm Post subject: Integer to Character type casting |
|
|
Try this...
mycharvar='text'//char(J+32)
Dan Rhea |
|
Back to top |
|
|
|