Continue to Site

# HEX to ASCII Conversion using C

Hello Everyone,

If you're willing to convert the values of 0 to 999 Decimal range (8 bit data) into their ASCII values then here is the program with explanation :

(and if you have 16 bit data then I'll post that too, just tell me)

Code:
void main()
{
unsigned char x,y,d1,d2,d3;
y = 0xFD;                  // y is your value, here I've take 0xFD  (253 in Decimal)
x = y / 10;                // after execution, x will have answer and y will have remainder
//(x=25 & y=3)
d1 = y % 10             // after execution y will have no change and d1 will have
//remainder (y=3 & d1=3)
// means you've got d1;your LSD (Least Significant Digit)
d2 = x % 10             // after execution x will have no change and d2 will have
//remainder (x=25 & d2=5)
// means you've got d2;your middle Digit
d3 = x / 10;             // after execution d3 will have answer and x will have remainder
//(d3=2 & x=5)
// means you've got d3;your MSD (Most Significant Digit)

// now we need to display all three digits 'd3=2' , 'd2=5' & 'd1=3'.
// (Here you'll never get d1,d2,d3 greater than 9. If you get then there must be a mistake //anywhere in program.)
// Now to convert them into the ASCII you need to add 30H in these values

d1+=0x30;
d2+=0x30;
d3+=0x30;

// Now you have 'd3=0x32' , 'd2=0x35' & 'd1=0x33'

// And these new values are the ASCII Code of your actual numbers
(Remember : by adding 30H  you get ASCII)

// Now just send these variables to LCD and display them

// Hope this helps you

}

Normally to get no.s displayed people often uses "sprintf" function in C, but it occupies a large space in your memory.
So this one is the better option to do so.

Thanks :-D

this is for beginner but still very usefull!

I got myself stuck with either using sprintf or converting. I choosed converting and I made this function:
Code:
char * NumberToASCII(double num){
// The return string may have max 21 chars (max writable with double types) + 1 null ending ( '\0' )
unsigned char RetString[22], pos=0;

if (num == 0){
// if num = 0 skip any operation and return the string "0"
RetString[pos] = '0';
} else{
// the number is not 0
/* putting the "minus" and "plus" signs in front of the number */
if (num < 0){
// put the minus sign
RetString[pos] = '-';
pos++;	// the number will start after the "minus" sign
} // else if (num > 0) {
// in most nations they won't put anything in front of positive
// numbers, while some others they put a "plus" sign.
// Decomment the else lines if needed
// RetString[pos] = '+';
// pos++;	// the number will start after the "plus" sign
//}

// getting the ASCII value
while (num != 0){ // num will become 0 after getting all the reminders
// the ascii value of each char composing the number
// is the reminder of a division by the radix added to char '0'
// we calculate the reminder by using the modulus operator: %
RetString[pos] = (num %10) + '0';
// dividing the number by 10 to prepare the next reminder at next cycle
num /= 10;
// setting the next position in the string
pos++;
}
}
// if you need a null-ended string decomment the next line
// RetString[pos] = '\0';
return RetString;
}
This function will return a char pointer to the ASCII string of the number and will also put the "minus" and "plus" signs in front of the number, when required. You can write any signed integer writable within the double range (usually it is 64 bit long). I hope it'll be helpful to you and others as it is to me

### Blog entry information

Author
Jigar 4 Electronics