Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Base Conversion

rahzan
New Contributor I
645 Views
Does anyone know of a Base conversion routine (source or library) that can be imemdiately used in CVF?

I had assumed that at least looking up a certain bit (i.e. base-2) of a given integer would be easily available but I cannot find this even in IMSL.

Thanks,
Tim
0 Kudos
2 Replies
Steven_L_Intel1
Employee
645 Views
If you want a particular bit, see the BTEST and IBITS intrinsics. I'll let others comment on base conversion.

Steve
0 Kudos
rahzan
New Contributor I
645 Views

module Base
integer(1),parameter:: maxBins=31
integer(4),parameter:: maxVal=2**maxBins-1
contains
!==================================================
!Converts inVal from decimal to base N (int-1)
! returns an array of size maxBins with the bit values
! on error sets the returned array to zero.
function D2Base(inVal, N) result (str)
implicit none
integer(1):: str(maxBins)
integer(4), intent(in):: inVal
integer(1), intent(in):: N
integer(4)::M
integer(1)::i

str=0
if(N<=0 .or. N>9 .or. inVal<=0 .or. inVal>maxVal)return
M=inVal
i=1
do while(M>=N)
str(i)=Mod(M,N)
M=M/N
i=i+1
enddo
str(i)=M
end function D2Base
!======================================================
integer(4) function Base2D(str, N) result (dec)
integer(1), intent(in):: str(maxBins),N
integer(1)::i
dec=0
do i=1,maxBins
!if(str(i)>=N)then; dec=0; return; endif
if(str(i)==0)cycle
dec=dec+str(i)*N**(i-1)
enddo
end function Base2D
!===================================================

end module Base
!=======================================================

use base
integer(1):: str(maxBins),b=8
m=23434
str=D2Base(M, b)
write(*,'(i1)')(str(i),i=maxBins,1,-1)
write(*,*)Base2D(str, b)
pause
end

0 Kudos
Reply