Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

3 byte ascii to 16 bit hex converter

Status
Not open for further replies.

dr pepper

Well-Known Member
Most Helpful Member
I need to convert 3 ascii characters 0-999 into a 16 bit hex value, before I attempt to write some code does anyone know of an asm file to do this?
 
Yes I've nicked aload from them, but they dont do ascii to binary, but they do have a bcd to binary, so I'll use that, all I need to do to convert ascii to bcd is subtract dec 48.
 
Yes I did it, it works, but there was an anomaly.
I convert from ascii into bcd by adding "0" (subtracting 1's complement ascii zero), and it does convert, however the bcd to binary conversion process messes up the higher 4 bits of the bcd input values, which then screws up the rest of the code.
I got round this by anding the higher 4 bits of the unpacked bcd values going into the bcd to hex converter after conversion is done.
Success, the authors of the conversion code obviously never thought of cleaning up the higher 4 bits.
 
You have not made it clear whether you want to convert the individual characters in an ascii representation, say for example, the "789" to 7, 8, and 9 in hex or whether you want the value 789 (decimal seven hundred eighty nine) converted to hex (0x315) or binary (b'1100010101').

One of the piclist programs does the former. Your compiler does the latter (or doesn't care).

John

Sorry, we cross posted. I'll wait until it's clear to me what you want.
 
Straightforward 100*d1 + 10*d2 + d3 shouldn't be that bad:

Code:
; first digit assumed to be 0, 1, or 2. It won't fit otherwise
clrf wreg
btfsc digits, 1
addlw 0x64 ; add 100
btfsc digits, 2
addlw 0xc8 ; add 200
movwf result
 
; second digit
movf digits+1,w
sublw 0x30 ; subtract '0'
rlf wreg,w ; 2*d
addwf result,f
rlf wreg,w ; 4*d
rlf wreg,w ; 8*d
addwf result,f
 
; third digit
movf digits+2,w
sublw 0x30; subract '0'
addwf result,f
 
3 bytes of ascii in 000-999, 2 bytes of hex out 0 - 3e7.
Sussed it now (got your crossed post), I converted the ascii into bcd by subtracting ascii for 0, then used one of piclist's bcd to hex converter, like I said the issue I had was that I use the bcd values elesewhere in the code which messed up as the bcd to hex converter allthough it doesnt mess up the bcd value it doesput hash in the higher nibble, I sorted this just by anding the bcd values after converting.
Thanks for the help guys, your code is effeicient north guy, I clipboarded that.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top