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, intrinsic :: ISO_FORTRAN_ENV, only: &
CSZ => CHARACTER_STORAGE_SIZE, &
stderr => error_unit
use :: M_anything, only : bytes_to_anything, anything_to_bytes
implicit none
character(len=1), allocatable :: chars(:)
character(len=:), allocatable :: line
character(len=:), allocatable :: lines(:)
integer :: ints(10)
integer :: i, int
integer,allocatable :: somesize(:)
call header('integer array to bytes')
chars = anything_to_bytes([(i*i, i=1, size(ints))])
write (*, '(/,4(1x,z2.2))') chars
call bytes_to_anything(chars, ints)
write(*,*)'and bytes back to integer array'
write (*, '(/,*(g0,1x))') ints
call header('integer scalar to bytes')
chars = anything_to_bytes(1234)
write (*, '(/,"CHARS=",*(1x,z2.2))') chars
call bytes_to_anything(chars, int)
write(*,*)'and bytes back to integer scalar'
write (*, '(/,"INT=",*(g0,1x))') int
call header('a string')
chars = anything_to_bytes('this is a string')
write (*, '(/,"CHARS=",*(1x,z2.2))') chars
write (*, '(/,"CHARS=",*(g0,1x))') chars
! string must be long enough to hold chars
line=repeat(' ',size(chars))
call bytes_to_anything(chars, line)
write (*, '(/,"LINE=",*(g0,1x))') line
call header(&
'a string array (have to know length or size you wish to return to)')
chars = anything_to_bytes([character(len=4) :: 'a', 'bb', 'ccc' ])
write (*, '(/,"CHARS=",*(1x,z2.2))') chars
write (*, '(/,"CHARS=",*(g0,1x))') chars
! string must be long enough to hold chars, and have enough elements
! can just return as a scalar string if unknown length
lines=[repeat(' ',size(chars))]
! of for that matter just work with the chars(1) array,
! but assuming know length in this case
lines=[(repeat('#',4),i=1,3)]
call bytes_to_anything(chars, lines)
write (*, '(/,"LINES=",*("[",g0,"]",1x:))') lines
call header('calculating size to allocate for non-string types')
! make sure array is of sufficient size to hold results
chars = anything_to_bytes([11,22,33,44])
write (*, '(/,"CHARS=",*(1x,z2.2))') chars
allocate(somesize(size(chars)/(storage_size(0)/CSZ)))
call bytes_to_anything(chars, somesize)
write (*, '(/,"SOMESIZE=",*("[",g0,"]",1x:))') somesize
contains
subroutine header(line)
character(len=*),intent(in) :: line
write(*,'(*(a))')'#',repeat('=',len(line)+2),'#'
write(*,'("|",1x,a,1x,"|")') line
write(*,'(*(a))')'#',repeat('=',len(line)+2),'#'
end subroutine header
end program demo_bytes_to_anything
Results:
> #========================#
> | integer array to bytes |
> #========================#
>
> 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
> and bytes back to integer array
>
> 1 4 9 16 25 36 49 64 81 100
> #=========================#
> | integer scalar to bytes |
> #=========================#
>
> CHARS= D2 04 00 00
> and bytes back to integer scalar
>
> INT=1234
> #==========#
> | a string |
> #==========#
>
> CHARS= 74 68 69 73 20 69 73 20 61 20 73 74 72 69 6E 67
>
> CHARS=t h i s i s a s t r i n g
>
> LINE=this is a string
> #====================================================================#
> | a string array (have to know length or size you wish to return to) |
> #====================================================================#
>
> CHARS= 61 20 20 20 62 62 20 20 63 63 63 20
>
> CHARS=a b b c c c
>
> LINES=[a ] [bb ] [ccc ]
> #===================================================#
> | calculating size to allocate for non-string types |
> #===================================================#
>
> CHARS= 0B 00 00 00 16 00 00 00 21 00 00 00 2C 00 00 00
>
> SOMESIZE=[11] [22] [33] [44]
John S. Urban
MIT
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=1), | intent(in) | :: | chars(:) | |||
| class(*), | intent(out) | :: | anything(:) |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=1), | intent(in) | :: | chars(:) | |||
| class(*), | intent(out) | :: | anything |