# Hex to ASCII conversion in 8051

Microprocessor8085

Now we will see how to convert Hexadecimal number to its ASCII equivalent using 8051. This program can convert 0-9 and A-F to its ASCII value.

We know that the ASCII of number 00H is 30H (48D), and ASCII of 09H is39H (57D). So all other numbers are in the range 30H to 39H. The ASCII value of 0AH is 41H (65D) and ASCII of 0FH is 46H (70D), so all other alphabets (B, C, D, E) are in the range 41H to 46H.

Here we are providing hexadecimal digit at memory location 20H, The ASCII equivalent is storing at location 30H.

Value

.
.
.
20H
0EH
21H

.
.
.

## Program

MOVR0,#20H; Initialize the address of the data
MOVA,@R0; Get the data from an address, which is stored in R0
MOVR2,A;Store the content of A into R2
CLRC; Clear the Carry Flag
SUBBA,#0AH;Subtract 0AH from A
JCNUM ; When a carry is present, A is numeric
NUM:    MOVA,R2; Copy R2 to A
STORE:  MOVR0,#30H; Point the destination location
MOV@R0,A; Store A content to the memory location pointed by R0
HALT:   SJMPHALT

The logic behind HEX to ASCII conversion is very simple. We are just checking whether the number is in range 0 – 9 or not. When the number is in that range, then the hexadecimal digit is numeric, and we are just simply adding 30H with it to get the ASCII value. When the number is not in range 0 – 9, then the number is range A – F, so for that case, we are converting the number to 41H onwards.

In the program, at first, we are clearing the carry flag. Then subtracting 0AHfrom the given number. If the value is numeric, then after subtraction the result will be negative, so the carry flag will be set. Now by checking the carry status, we can just add 30H with the value to get ASCII value.

In other hands when the result of the subtraction is positive or 0, then we are adding 41H with the result of the subtraction.

Value

.
.
.
20H
0EH
21H

.
.
.
30H
45H
31H

.
.
.