Mortezanamvar
Mortezanamvar

Reputation: 69

Read multiple hex values from single line

I want to read a set of hex values which are arranged in a line, but number of spaces between each value is not determined. How I can read this in Fortran 90. For instance:

2F4----33--6B5----------4DB3

program ReadSomeHex

implicit none


integer:: Decval , Decval2 , Decval3 , Decval4 , i

Open(1,File='Input.txt')

Open(2,File='Output.txt')


read(1,'(Z4,Z4,Z4,Z4)') Decval , Decval2 , Decval3 , Decval4

write(*,*)        Decval , Decval2 , Decval3 , Decval4

end program ReadSomeHex

Upvotes: 1

Views: 266

Answers (1)

With "unformatted" input (by which I now mean formatted (i.e. text) fortran input which doesn't follow a specific fixed format; not to be confused with actual unformatted input [NB]) your best option is probably to read in the hexa values as strings, then perform a fixed-format (rather than list-directed) read on the strings:

program ReadSomeHex

implicit none

character*4 :: Hexval, Hexval2, Hexval3, Hexval4
integer:: Decval , Decval2 , Decval3 , Decval4 

Open(1,File='Input.txt')
Open(2,File='Output.txt')

read(1,*) Hexval, Hexval2, Hexval3, Hexval4
read(Hexval,'(Z4)') Decval
read(Hexval2,'(Z4)') Decval2
read(Hexval3,'(Z4)') Decval3
read(Hexval4,'(Z4)') Decval4

write(*,*)        Decval , Decval2 , Decval3 , Decval4

end program ReadSomeHex

Note that you should make sure that each of your hexa numbers are at most 4 characters long.

[NB] If you don't specify otherwise during Open, fortran assumes you mean formatted input/output, i.e. a text file.

Upvotes: 3

Related Questions