converting ascii hex to binary (was Re: [sdiy] TMS7000 disassembler ? )
The Old Crow
oldcrow at oldcrows.net
Tue Feb 26 01:11:20 CET 2002
On Mon, 25 Feb 2002 CHoaglin at aol.com wrote:
> while we're on the subject of disassemblers, does anybody know how to go
> about converting an ASCII hex
>
> ie: <ADDRESS>-----<HEX>------<ASCII REPRESENTATION>
> file into a raw binary file containing only the binary representation of
> the hex?
Hm..you want to convery hex-ascii to binary? In what language?
Since each ascii digit takes up a byte, you want to collapse every digit
pair into one byte. The usual way to do this is (hope you can follow PIC
code):
Note, I am assuming the input ascii digits are known to be 0 to 9 or A
to F.
; W has asciihex digit to convert
;
addlw -'A' ;W-'A', is W between '0' and '9' or 'A' and 'F'?
skpc ;C flag=1 means W-'A' where W between 'A' and 'F'
addlw 'A'-10+'0' ;If W is 0 to 9, W=W-'A'-10+'0' gets 0x00 to 0x09
addlw 10 ;If W is A to F, W=W-'A'+10 yields 0x0A to 0x0F
That converts one of the 2 digits of a byte. Do the same thing on the
other. Then simply take the first digitx16 (left shift x4) and add it to
the second for the final result.
Sort of like:
; do MSD
movf MSD,W ;MSD is a variable name I made up = some ram loc.
addlw -'A'
skpc
addlw 'A'-10+'0'
addlw 10
movwf MSBD ;Another made-up varname
; do LSD (druuuug humor)
movf LSD,W ;LSD is a var...blahblah
addlw -'A'
skpc
addlw 'A'-10+'0'
addlw 10
movwf LSBD ;LSBD blah
;form final byte
rlcf MSBD,f ;MSBDx2
rlcf MSBD,f ;x4
rlcf MSBD,f ;x8
rlcf MSBD,w ;x16
andlw 0xF0 ;make sure low nyb clean
iorwf LSBD,W ;form the byte
movwf BINBYTE ;hooray, another varname which is result
If I was less lazy, I could shrink that code scrap a little more, but
then it wouldn't be as obvious.
Crow
/**/
More information about the Synth-diy
mailing list