# Req - Binary to ASCII

Status
Not open for further replies.

#### jimbies

##### Member level 2
Does any one have a ready C routine to convert Binary > ASCII or
Binary > Decimal > Ascii ? I need to display an A to D result on a terminal. Thanks.

#### pardal

##### Member level 5
You can try a free product called GMT
(http://gmt.soest.hawaii.edu/). If you are on windows there is a pre-compiled version, otherwise you have to compile it yourself. Whithin it there
is a module called gmtconvert that does what you want.

#### Gomez

##### Full Member level 2
See source code for printf/sprintf.

Bye Gomez

#### Bus Master

##### Full Member level 3
You can follow these steps, very simple and effective:

1- Convert the binary no. to BCD format, this leads to a groups of 4 bits in each group, each group value is from 0 to 9.

2- For every group, ADD 30 (decimal) to the 4bits value ==> ASCII of the digit. (e.g. A value of '1' will be '31' which is the ASCII of digit '1').

3- Repeat step 2 for every group, ==> ASCII values for digits to be generated and ready to be sent to LCD.

Hope this helps.

#### Darklord

##### Junior Member level 3
Can't find the gmtconvert zip file. Where is it exactly? Has anyone implemented it in C?

#### artem

Hi

Below is the one of the possible methods :
void num2str
(
INT16U tNumber, // Number to be converted
INT8U *pString // Pointer to sstring where converted number will be stored
)
{
INT8S tpos;

while(*pString != 0) // Goto end of string
pString++;
// Find number of bytes to encode number
if (tNumber < 10) tpos = 0; // 1 digit
else if(tNumber < 100) tpos = 1; // 2 digits
else if(tNumber < 1000) tpos = 2; // 3 digits
else if(tNumber < 10000) tpos = 3; // 4 digits
else tpos = 4; // 5 digits

*(pString + tpos + 1) = 0; // Put ending string delimiter

for(;tpos >= 0 ; tpos--) // Convert number to string in loop
{
*(pString + tpos) = tNumber%10 + 0x30; // Calculate ascii digit and store it in string
tNumber = tNumber/10; // Divide the number by 10
}
}

It is for hex to BCD ASCII conversion . But it was right to use printf or sprintf as those are part of standard library , if you do not have some restrictions applying to theses functions (part of C standard ibrary ).
The hex to Hex ASCII is much more easy .

#### db69

##### Junior Member level 1
use sprinf ansi c function.

char *s;
unsigned int NumberToConvert;

sprintf(s,"%u",NumberToConvert);

Status
Not open for further replies.