Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Program for converting ASCII code into Hex

Status
Not open for further replies.

sangmeshwar

Junior Member level 3
Joined
Apr 25, 2006
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,476
Please give detail 8051 microcontroller program for converting ASCII code into HEX.

Thanks

Sangmeshwar
 

Here is the code (A is input and output):

Code:
; ==============================================
; ASCII_Hex conversion routine...

ASC_Hex:	CJNE		A, #'0', Ch_1	; Test for ASCII numbers..
Ch_1:		JC		Ch_Bad		; Character is less than a '0'..
		CJNE		A, #'9'+1, Ch_2	; Test value range..
Ch_2:		JC		Ch_Val_109		; Character is between 0 and 9..

		CJNE		A, #'A', Ch_3		; Test for upper case hex letter..
Ch_3:		JC		Ch_Bad		; Character is less than 'A'..
		CJNE		A, #'F'+1, Ch_4	; Test value range..
Ch_4:		JC		Ch_Val_AF		; Character is between A and F..

Ch_Bad:	SETB		ErrFlag5		; Character is not a Hex number..
		LJMP		ASC_Hex_Exit

Ch_Val_AF:	CLR		C
		SUBB		A, #07h
Ch_Val_109:	CLR		C
		SUBB		A, #'0'
		CLR		ErrFlag5

ASC_Hex_Exit:RET

; ===================================================

Regards,
IanP
 

Ascii to hex - simple :)

Subtract hex 30 - check if result > 9 , if it is then subtract a further 7 - thats al that is required
 

Forgive me, I was feeling lazy and needed to do this function myself, so found these posts.


In the end I decided to write it myself as ascii - 0x30 does give you 0 to 9 but why - 7 ?

This "Simple" routine is also case freindly so 9E or 9e will both work, but there is no error checking.

Code:
	; ***************************************************
	; * ASCII to HEX                                    *
	; * takes an ascii character and converts it to hex *
	; ***************************************************
	;
ASCIItoHEX:
	setb	ACC.5				; Strip case, lower case ascii 
	subb	a,#0x57				; value a - f will not carry
	jnc	ASCIIDone			; if not carry then we have converted the nibble
	add	a,#0x27				; Convert back to 0 - 9
ASCIIDone:
	ret
 

The - 7 is only required if the data contains letters of the alphabet. If you examine an ascii table you will see that between the numbers and letter are 7 symbols that need to be skipped. Assuming that this is hex data it will contain A - F and you will therefore need to - 7
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top