• entries
17
28
• views
32,023

# Hex to Ascii String

1,837 views

I saw a topic on 6502.org about converting byte to hex string. I remembered Lee Davison's code shorts about converting 0 to 15 to ascii 0 to 15 by using decimal mode. I know Lee passed along this year so RIP Lee, I didn't know you but you seemed like a genius to me.

Using his basic code I came up with this for converting a byte:

```;HEX to ASCII
;  A = entry value
sed        ;2  @2
tax        ;2  @4
and #\$0F   ;2  @6
cmp #9+1   ;2  @8
tay        ;2  @12
txa        ;2  @14
lsr        ;2  @16
lsr        ;2  @18
lsr        ;2  @20
lsr        ;2  @22
cmp #9+1   ;2  @24
cld        ;2  @28
;  A = MSN ASCII char
;  Y = LSN ASCII char```

I ran a short test program for all values and it passed. Twenty eight cycles is not too bad. It's kind of a cool way to convert a byte like this. This could make for a nice project for communicating out of the 2600 I/O port and displaying to a LCD panel in useless but fun way. Maybe you could have two 2600's with the LCD in between them to see the bytes fly by.

Just need to make a conversion routine from ASCII back to hex.

Edit: Came up with this off the top of my head. Tested it for a few values and seems to work. It can only handle ASCII '0' to '9', and 'A' to 'F' (capitals).

```;ASCII to HEX
;  A = MSN ASCII char
;  Y = LSN ASCII char

cmp #\$39+1      ;2  @2
bcc .highNybble ;2³ @4/5
sbc #7          ;2  @6
.highNybble:
asl             ;2  @8
asl             ;2  @10
asl             ;2  @12
asl             ;2  @14
sta temp        ;3  @17
tya             ;2  @19
cmp #\$39+1      ;2  @21
bcc .LowNibble  ;2³ @23/24
sbc #7          ;2  @25
.LowNibble:
and #\$0F        ;2  @27
eor temp        ;3  @30 worse case, 28 best case```

• 1

There are no comments to display.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.