por henriquethiesen » 10 Nov 2007 19:24
As funções não estão prontas mas a idéia de como separar e unir bytes segue abaixo, testado no keil.
typedef union
{
long i;
char byte[4];
}u_long;
char LongToChar(long *ptr, char Nbyte)
{
return *((unsigned char*)ptr + Nbyte-1);
}
void main()
{
char Byte1;
char Byte2;
char Byte3;
char Byte4;
u_long Teste;
long Value = 0x12345678;
// convertendo de Long para char
Byte1 = LongToChar(&Value, 1); // Byte1 = 0x12
Byte2 = LongToChar(&Value, 2); // Byte2 = 0x34
Byte3 = LongToChar(&Value, 3); // Byte3 = 0x56
Byte4 = LongToChar(&Value, 4); // Byte4 = 0x78
// convertendo de char para long
Teste.byte[0] = Byte1;
Teste.byte[1] = Byte2;
Teste.byte[2] = Byte3;
Teste.byte[3] = Byte4;
// Agora Teste.i contém 0x12345678
Value = Teste.i;
}