	subroutine replace(ibeg,iend,iflag,length)
	implicit integer (a-z)

	real*4 single_float
	real*8 double_float

	byte buffer(32768),edit_buffer(32768),byte_value,long_array(4),
     1	     word_array(2),single_array(4),double_array(8)
	equivalence (long_array(1),ivalue)
	equivalence (word_array(1),ivalue1)
	equivalence (single_array(1),single_float)
	equivalence (double_array(1),double_float)

	common /buf/ buffer,edit_buffer,length_total

	character*255 substitution

	common /sub/ substitution

	dimension lengths(8)
!
! length in bytes of  A O L W B F D H
!
	data lengths /1,1,4,2,1,4,8,4/
	
	if (length.le.0) then
		type 5
 5		format(/' Invalid replacement'/)
		return
	 endif
!
! figure out byte numbers for starting replacement
!
	call startbyte(ibeg,istart_rep,max_end)

	if (istart_rep.le.0) return

	if (iend.gt.max_end) iend=max_end

	if (iflag.eq.1) then				! ascii substitution
		
		do i=1,length
		 	edit_buffer(istart_rep+i-1)=
     1				ichar(substitution(i:i))
		 enddo
	 endif

	if (iflag.eq.2) then				! octal substitution
!
! unsigned octal one byte, ignore blanks in processing
!
		byte_value=0
		istat=ots$cvt_to_l(substitution(1:length),byte_value,
     1			%val(1),%val(1))
		call check(istat)
		if (istat.le.0) return
		call replace_bytes(ibeg,iend,lengths(iflag),
     1			istart_rep,byte_value)
	 endif

	if (iflag.eq.3) then				! longword
!
! signed I*4, ignore blanks, 4 bytes
!
		ivalue=0
		istat=ots$cvt_ti_l(substitution(1:length),ivalue,
     1					%val(4),%val(1))
		call check(istat)
		if (istat.le.0) return
		call replace_bytes(ibeg,iend,lengths(iflag),
     1					istart_rep,long_array)
	 endif

	if (iflag.eq.4) then				! word
		ivalue1=0
!
! signed I*2 two bytes, ignore blanks
!
		istat=ots$cvt_ti_l(substitution(1:length),ivalue1,
     1					%val(2),%val(1))
		call check(istat)
		if (istat.le.0) return
		call replace_bytes(ibeg,iend,lengths(iflag),
     1					istart_rep,word_array)
	 endif

	if (iflag.eq.5) then				! signed byte
		ivalue=0
!
! signed L*1 one byte, ignore blanks
!
		istat=ots$cvt_ti_l(substitution(1:length),ivalue,
     1					%val(1),%val(1))
		call check(istat)
		if (istat.le.0) return
		call replace_bytes(ibeg,iend,lengths(iflag),
     1					istart_rep,word_array)
	 endif
	if (iflag.eq.6) then				! floating
		single_float=0.
!
! R*4, 4 bytes, ignore blanks, underflow will error, no rounding
!
		istat=ots$cvt_t_f(substitution(1:length),single_float,
     1					,,%val(13))
		call check(istat)
		if (istat.le.0) return
		call replace_bytes(ibeg,iend,lengths(iflag),
     1					istart_rep,single_array)
	 endif

	if (iflag.eq.7) then				! double floating
		double_float=0.
!
! R*8, 8 bytes, ignore blanks, underflow will error, no rounding
!
		istat=ots$cvt_t_d(substitution(1:length),double_float,
     1					,,%val(13))
		call check(istat)
		if (istat.le.0) return
		call replace_bytes(ibeg,iend,lengths(iflag),
     1					istart_rep,double_array)
	 endif
	if (iflag.eq.8) then				! hex (default)
!
! hex, 4 bytes, ignore blanks
!
		ivalue=0
		istat=ots$cvt_tz_l(substitution(1:length),ivalue,
     1					%val(4),%val(1))
		call check(istat)
		if (istat.le.0) return
		call replace_bytes(ibeg,iend,lengths(iflag),
     1					istart_rep,long_array)
	 endif
	call special(edit_buffer,length_total,1,' ',1)
	return
	end

	subroutine check(istat)
	implicit integer (a-z)
	
	external ots$_inpconerr,ss$_normal
	
	if (istat.eq.%loc(ots$_inpconerr)) then
		type 10
 10		format(/' Input conversion error for data type '/)
		istat=-1
		return
	 endif
	
	if (istat.ne.%loc(ss$_normal)) then
		type 20,istat
 20		format(/' Unknown error in coversion, code is ',z8/)
		istat=-1
		return
	 endif
	end
	
	subroutine replace_bytes(ibegin,iend,length,istart,byte_array)
	byte byte_array(length),buffer(32768),edit_buffer(32768)

	common /buf/ buffer,edit_buffer,length_total

	imany=iend-ibegin+1
	if (imany.le.0) imany=1
	irep=0
	do i=1,imany
		do j=1,length
			edit_buffer(istart+irep+j-1)=byte_array(j)
		 enddo
		irep=irep+length
	 enddo
	return
	end
