Página 1 de 1
long char para 4 char

Enviado:
10 Nov 2007 12:03
por rebelk
nos pics para ler um byte de uma variavel int16 ou int32 ultiliza a função DADO_0=MAKE8 ( A,1);.
alguem poderiam me indicar umas funções que ler byte de uma variavel de 16 e 32 bits , e que tranforme dois char em um int , e quatro char em um int32 ( long int ).
em c keil!

Enviado:
10 Nov 2007 19:24
por henriquethiesen
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;
}

Enviado:
12 Nov 2007 06:40
por ze
2 char em um int não é só fazer (int)MSB*256+LSB?
com o pic já fiz o resultado do ad em int: ADRESH*256+ADRESL. (funcionou)
não se pode usar o mesmo princípio para o long? (vou testar)

Enviado:
01 Dez 2007 23:07
por rebelk
VALEU A TODOS DEU CERTO