encode_float32_base64_alloc Subroutine

public subroutine encode_float32_base64_alloc(values, n, output)

Encode float32 array as base64 string (allocatable output)

Arguments

Type IntentOptional Attributes Name
real(kind=real32), intent(in) :: values(:)
integer, intent(in) :: n
character(len=:), intent(out), allocatable :: output

Source Code

  subroutine encode_float32_base64_alloc(values, n, output)
    !! Encode float32 array as base64 string (allocatable output)
    use iso_fortran_env, only: int8, int32
    implicit none
    real(real32), intent(in) :: values(:)
    integer, intent(in) :: n
    character(:), allocatable, intent(out) :: output

    integer(int8), allocatable :: bytes(:)
    integer(int32) :: ival
    integer :: i, j, nbytes

    nbytes = n * 4
    allocate(bytes(nbytes))

    do i = 1, n
       ival = transfer(values(i), ival)
       bytes((i-1)*4 + 1) = int(iand(ival, 255), int8)
       bytes((i-1)*4 + 2) = int(iand(ishft(ival, -8), 255), int8)
       bytes((i-1)*4 + 3) = int(iand(ishft(ival, -16), 255), int8)
       bytes((i-1)*4 + 4) = int(iand(ishft(ival, -24), 255), int8)
    end do

    call base64_encode_bytes(bytes, nbytes, output)
    deallocate(bytes)

  end subroutine encode_float32_base64_alloc