Jump to content
  • entries
    17
  • comments
    28
  • views
    32,096

Hex to Ascii String


Omegamatrix

1,881 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
  adc #$30   ;2  @10
  tay        ;2  @12
  txa        ;2  @14
  lsr        ;2  @16
  lsr        ;2  @18
  lsr        ;2  @20
  lsr        ;2  @22
  cmp #9+1   ;2  @24
  adc #$30   ;2  @26
  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
 
  • Like 1

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...