×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# CHAR and ICHAR functions

## CHAR and ICHAR functions

(OP)
Hello,

I am trying to convert a high number (e.g. 123456789) into a character and then reconvert that character into the first number in another file.
To do this, I am using CHAR and ICHAR functions.
Using:

WRITE(*,*) ICHAR(CHAR(123456789,4),4)

I get the number 123456789

However, when I try to separate the steps

CHARACTER*1 TMPC1
INTEGER*4 TMPI1
INTEGER*4 NUMBER

NUMBER = 123456789

TMPC1 = CHAR (NUMBER,4)
TMPI1 = ICHAR (TMPC1,4)

WRITE(*,*) TMPI1

The ICHAR function only allows a character input of length 1 !!
I get the number 21. At first this was a bit confusing. It became clear when I used this program to convert the numbers 0, 1, 2, 3, ..., 300
The conversion was flawless until number 254, and then reseted:

253 > 253
254 > 254
255 > 255
256 > 0
257 > 1

This is due to the limited number of ASCII characters, which is 255.
Any thoughts on how to convert a number larger than 255, and retrieve the same number?

Many thanks!

### RE: CHAR and ICHAR functions

Which compiler are you using? I've never seen ICHAR(number, kind) before.

### RE: CHAR and ICHAR functions

What are you actually trying to achieve. Are you trying to convert a number into character form and then retrieve it from character form and convert it back to a number?

### RE: CHAR and ICHAR functions

(OP)
I am using the gfortran compiler.

xwb (Programmer)
What are you actually trying to achieve. Are you trying to convert a number into character form and then retrieve it from character form and convert it back to a number?

That's exactly what I am trying to do here.

### RE: CHAR and ICHAR functions

The right way is to use READ and WRITE instead of ICHAR and CHAR.

Indeed, ICHAR/CHAR manage only bytes (1 byte = 8 bits = integer from 0 to 255 = a single character)

#### CODE --> fortran

program test
integer :: i,j
character(9) :: c
i=123456789
write(c,'(i0)') i ! initializing the string c
print*,c
print*,j
end program 

Result :

123456789
123456789

François Jacq

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!