por Iran » 28 Mai 2007 12:03
Desculpem minha burrice mas ainda não entendi.
Uma variável char (8 bits) com um número em hexa, por exemplo 0x47, que corresponde em decimal a 71 deve ser convertida para ascii antes de ser enviada para o display, portanto deverá ser enviado 0x30 0x37 0x31, onde:
0x30 código ascii do 0
0x37 código ascii do 7
0x31 código ascii do 1
É isso mesmo ?
A rotina abaixo faz isso ? Neste caso SourceLength = 1 e Destination[3] ?
//This function turns a charactor array in binary at Source to an ASCII string at destination
//Destination must be (Source * 2 + 1) bytes long or you will get buffer overruns
void Bin2Ascii(unsigned char Source[], int SourceLength, unsigned char Destination[])
{
Destination[SourceLength * 2] = '\0';
while(SourceLength != 0)
{
Destination[SourceLength * 2 - 2] = Source[SourceLength - 1];
Destination[SourceLength * 2 - 1] = Source[SourceLength - 1];
Destination[SourceLength * 2 - 2] &= 0x0f;
Destination[SourceLength * 2 - 1] &= 0xf0;
Destination[SourceLength * 2 - 1] >>= 0x4;
if(Destination[SourceLength * 2 - 2] <= 0x9)
Destination[SourceLength * 2 - 2] |= 0x30;
else
{
Destination[SourceLength * 2 - 2] |= 0x60;
Destination[SourceLength * 2 - 2] -= 0x9;
}
if(Destination[SourceLength * 2 - 1] <= 0x9)
Destination[SourceLength * 2 - 1] |= 0x30;
else
{
Destination[SourceLength * 2 - 1] |= 0x60;
Destination[SourceLength * 2 - 1] -= 0x9;
}
SourceLength--;
}
}
Grato,
Zé Iran