hex2int Function

public pure function hex2int(hex)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: hex

Return Value integer


Called by

proc~~hex2int~~CalledByGraph proc~hex2int aoc_utilities::hex2int proc~go~2 problem_18::go proc~go~2->proc~hex2int program~problem_18 problem_18 program~problem_18->proc~go~2

Source Code

    pure integer function hex2int(hex)
        character(len=*),intent(in) :: hex
        integer :: i, n, ipower

        ! 70c71 -> 461937

        n = len(hex)
        hex2int = 0

        !base 16 (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f)
        ipower = -1
        do i = n, 1, -1
            ipower = ipower + 1
            associate(c => hex(i:i))
                if (c>='a' .and. c<='f') then
                    hex2int = hex2int + (10+iachar(c)-iachar('a'))*(16**ipower)
                else
                    hex2int = hex2int + (iachar(c)-iachar('0'))*(16**ipower)
                end if
            end associate
        end do

    end function hex2int