define(EOS,0) # byte function hexbin(hex,binary) # ################################################################################ # Converts the ascii hex character string stored in HEX to an integer #binary value BINARY. HEX can be at most 4 bytes long. If it is less than 4, #then the string must be terminated by a null byte. Upper and lower case #letters are handled identically by the routine. # If the conversion was succesful, then HEXBIN returns true, else it #returns false. ################################################################################ integer binary # byte hex(4), nibble # # ns=0 # # of bits to left shift value # in order to place calculated nibble # in correct nibble of output value binary=0 # initialize the return value for (len=0; len<4&hex(len+1)~=EOS; len=len+1) { } # Calculate string length for (I=len; I>=1; I=I-1) { # start with ls nibble nibble=hex(I) # if (nibble>='0'&nibble<='9') # convert nibble to binary nibval=nibble-'0' # value is 0-9 else # if (nibble>='A'&nibble<='F') # nibval=nibble-'A'+10 # value is 10-15 else # if (nibble>='a'&nibble<='f') # lower case alpha nibval=nibble-'a'+10 # value is 10-15 else # return(.false.) # else error binary=ishft(nibval,ns)+binary # ns=ns+4 # bit pointer } # return(.true.) # end # # subroutine binhex(binary,hex) # ################################################################################ # Converts the integer binary value BINARY to a 4 byte string of ascii #hex characters. ################################################################################ byte hex(4), hextbl(16) # integer binary, bintmp # data hextbl/'0','1','2','3','4','5','6','7', # '8','9','A','B','C','D','E','F'/ # # bintmp=binary # copy the input value so unmodified for (I=4; I>=1; I=I-1) { # hex(I)=hextbl(iand(bintmp,15)+1) # bintmp=ishft(bintmp,-4) # shift next nibble into low order nibble } # return # end # # subroutine padhex(hex,hex4) # ################################################################################ # Converts the arbitrary length ASCII hex character string in HEX to a #4 character byte string and stores the result in HEX4. HEX may be up to #four characters in length and if less than 4, the string must be terminated by #a null byte. ################################################################################ byte hex(4), hex4(4) # integer shift # # Calculate string length for (len=0; len<4&hex(len+1)~=EOS; len=len+1) { } npad=4-len # # of 0's to prepend for (I=1; I<=4; I=I+1) { # if (I<=npad) # this byte converts to a '0' hex4(I)='0' # else # hex4(I)=hex(I-npad) # else copy byte } # # return # end # # byte function hexsub(min,sub,diff) # ################################################################################ # Function performs a 16 bit subtraction of two ascii hex values. The hex #SUBtrahend is subtracted from the hex MINuend and returned in the hex #DIFFerence. MIN and SUB must be ASCII hex values up to four characters in #length and if less 4 characters the strings MUST be terminated with a null #byte. DIFFerence is always returned as a 4 character string. # # The function returns false if a negative value results from the #subtraction (there are no negative hex values allowed), or if either the #MIN or the SUB are illegal hex values. # # The function splits the minuend and the subtrahend into high #and low bytes, subtracts each independently, checks for a borrow condition, #converts the high and low order results into hex, and then concatenates the #two strings to form the ASCII hex difference. ################################################################################ byte min(4), sub(4), diff(4), hmin(4), hsub(4) # byte hexbin, min4(4), sub4(4) # integer borrow # # call padhex(min,min4) # convert to four byte hex string call padhex(sub,sub4) # convert to four byte hex string # call copy(min4(3),hmin(1),3) # copy low order byte of minuend call copy(sub4(3),hsub(1),3) # copy low order byte of subtrahend if (~hexbin(hmin,ihmin)|~hexbin(hsub,ihsub)) # hex to binary conversion error return(.false.) # ildiff=ihmin-ihsub # subtract low order bytes # store in low order difference if (ildiff<0) { # borrow generated ildiff=ildiff+256 # borrow borrow=1 # set value to be borrowed } # else # borrow=0 # else set borrow to 0 # call copy(min4(1),hmin(1),3) # copy high order byte of minuend call copy(sub4(1),hsub(1),3) # copy high order byte of subtrahend # if (~hexbin(hmin,ihmin)|~hexbin(hsub,ihsub)) # hex to binary conversion error return(.false.) # ihdiff=ihmin-ihsub-borrow # subtract high order bytes, # subtract borrowed value, if any # store in high order difference if (ihdiff<0) # high order difference result is negative, return(.false.) # this is not allowed # call binhex(ildiff,hmin) # convert low order difference to hex call binhex(ihdiff,hsub) # convert low order difference to hex diff(1)=hsub(3) # copy result into array DIFF diff(2)=hsub(4) # diff(3)=hmin(3) # diff(4)=hmin(4) # # return(.true.) # end # # byte function hexadd(add,aug,sum) # ################################################################################ # Function performs a 16 bit addition of two ascii hex values. The hex #AUGend is added to the hex ADDend and returned in the hex SUM. #ADD and AUG must be ASCII hex values up to four characters in length and #if less 4 characters the strings MUST be terminated with a null byte. SUM #is always returned as a 4 character string. # # The function returns false if an overflow results from the addition, #or if either ADD or AUG are illegal hex values. # # The function splits the ADDend and the AUGend into high and low bytes, #adds each individually, checks for a carry condition, converts the high and #low order results into hex, and then concatenates the two strings to form #the ASCII hex SUM. ################################################################################ byte add(4), aug(4), sum(4), hadd(4), haug(4) # byte hexbin, add4(4), aug4(4) # integer carry # # call padhex(add,add4) # convert to four byte hex string call padhex(aug,aug4) # convert to four byte hex string # call copy(add4(3),hadd(1),3) # copy low order byte of addend call copy(aug4(3),haug(1),3) # copy low order byte of augend if (~hexbin(hadd,ihadd)|~hexbin(haug,ihaug)) # hex to binary conversion error return(.false.) # ilsum=ihadd+ihaug # add low order bytes # store in low order SUM if (ilsum>=256) { # carry generated ilsum=ilsum-256 # subtract value that will be carried carry=1 # set value to carry } # else # carry=0 # else set carry to 0 # call copy(add4(1),hadd(1),3) # copy high order byte of addend call copy(aug4(1),haug(1),3) # copy high order byte of augend # if (~hexbin(hadd,ihadd)|~hexbin(haug,ihaug)) # hex to binary conversion error return(.false.) # ihsum=ihadd+ihaug+carry # add high order bytes, # add carry, if any # store in high order sum if (ihsum>=256) # high order byte summation overflow, return(.false.) # this is not allowed # call binhex(ilsum,hadd) # convert low order sum to hex call binhex(ihsum,haug) # convert low order sum to hex sum(1)=haug(3) # copy result into array sum sum(2)=haug(4) # sum(3)=hadd(3) # sum(4)=hadd(4) # # return(.true.) # end #