bytes_to_anything(3f) - [M_anything] convert bytes(character)len=1):: array(:)) to standard types
(LICENSE:MIT)
subroutine bytes_to_anything(chars,anything)
character(len=1),allocatable :: chars(:)
class(*) :: anything
This function uses polymorphism to allow input arguments of different
types. It is used to create other procedures that can take many
argument types as input options and convert them to a single type
to simplify storing arbitrary data, to simplify generating data
hashes, ...
CHARS The input value is an array of bytes (character(len=1)).
ANYTHING May be of KIND INTEGER(kind=int8), INTEGER(kind=int16),
INTEGER(kind=int32), INTEGER(kind=int64),
REAL(kind=real32, REAL(kind=real64),
REAL(kind=real128), complex, or CHARACTER(len=*)
Sample program
program demo_bytes_to_anything
use M_anything, only : bytes_to_anything
use M_anything, only : anything_to_bytes
implicit none
character(len=1),allocatable :: chars(:)
integer :: ints(10)
integer :: i
chars=anything_to_bytes([(i*i,i=1,size(ints))])
write(*,'(/,4(1x,z2.2))')chars
call bytes_to_anything(chars,ints)
write(*,'(*(g0,1x))')ints
end program demo_bytes_to_anything
Results:
01 00 00 00 04 00 00 00 09 00 00 00 10 00 00 00 19 00 00 00 24 00 00 00 31 00 00 00 40 00 00 00 51 00 00 00 64 00 00 00 1 4 9 16 25 36 49 64 81 100
John S. Urban
MIT
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=1), | allocatable | :: | chars(:) | |||
class(*) | :: | anything(:) |
subroutine bytes_to_anything(chars,anything) character(len=1),allocatable :: chars(:) class(*) :: anything(:) select type(anything) type is (character(len=*)); anything=transfer(chars,anything) type is (complex); anything=transfer(chars,anything) type is (complex(kind=dp)); anything=transfer(chars,anything) type is (integer(kind=int8)); anything=transfer(chars,anything) type is (integer(kind=int16)); anything=transfer(chars,anything) type is (integer(kind=int32)); anything=transfer(chars,anything) type is (integer(kind=int64)); anything=transfer(chars,anything) type is (real(kind=real32)); anything=transfer(chars,anything) type is (real(kind=real64)); anything=transfer(chars,anything) #ifdef HAS_REAL128 type is (real(kind=real128)); anything=transfer(chars,anything) #endif type is (logical); anything=transfer(chars,anything) class default stop 'crud. bytes_to_anything(1) does not know about this type' end select end subroutine bytes_to_anything