Programmation C : 2 unsigned char dans 1 unsigned int.

Résolu
romain60112 Messages postés 70 Date d'inscription mercredi 1 octobre 2008 Statut Membre Dernière intervention 17 avril 2013 - 7 mai 2010 à 12:19
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 10 mai 2010 à 19:57
Bonjours a tous,

J'ai un petit soucis pour stocker 2 unsigned char dans 1 unsigned int.
Je dispose de :
un tableau d'unsigned int :
unsigned int configuration[10];

La structure du programme est la suivante :
#define mask_L 0x00FF
#define mask_H 0xFF00
unsigned int configuration[10];
void main ()
{
if(compteur == 1) // chaque seconde on vient incrémenter le tableau unsigned int
{
switch (cas)
{
case 1 :
configuration[0]&mask_L++; // <- Erreur, je ne sais pas comment faire sa
if((configuration[0]&mask_L) == 255)
{
(configuration[0]&mask_L) = 0;
((configuration[0]&mask_H)>>8)++; // <- Erreur, je ne sais pas comment faire sa

if(((configuration[0]&mask_H)>>8) == 255)
{
(configuration[0]&mask_L) = 0;
((configuration[0]&mask_H)>>8)=0;
configuration[1]&mask_L++; // <- Erreur, je ne sais pas comment faire sa
if((configuration[1]&mask_L) == 255)
{ // Full 16777215s 4660heures
}
}
}

case 2 :

((configuration[1]&mask_H)>>8)++; // <- Erreur, je ne sais pas comment faire sa
if(((configuration[1]&mask_H)>>8) == 255)
{
((configuration[1]&mask_H)>>8) = 0;
(configuration[2]&mask_L)++; // <- Erreur, je ne sais pas comment faire sa

if((configuration[2]&mask_L) == 255)
{
(configuration[2]&mask_L) = 0;
((configuration[2]&mask_H)>>8)++; // <- Erreur, je ne sais pas comment faire sa

if(((configuration[2]&mask_H)>>8)== 255)
{ // Full 16777215s 4660heures
}
}
}
case 3 : ...

}
}

Merci de votre aide.

Cordialement

Romain

3 réponses

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
7 mai 2010 à 15:40
Je fais sur du INT32 (unsigned int).

typedef union _DWPERSO {
struct {
WORD l;
WORD h;
};
DWORD val;
} DWPERSO;

void tstUnion()
{
char buf[24], *c;
DWPERSO dw[2];
// INIT LES 2 a 0
dw[0].val = 0;
dw[1].val = 0;


dw[0].h++; // INCREM PARTIE HAUTE DU 1er
dw[1].l--; // DECREM PARTIE BASSE DU 2eme

// AFFICHAGE PERSO DE TEST
c = bnultoa(dw[0].val, buf);
*c++ = 10;
*bnultoa(dw[1].val, c) = 0;
MessageBox(0, buf, szappname, 0);
}


ciao...
BruNews, MVP VC++
3
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
10 mai 2010 à 19:57
Mets un nom sur la struct:
typedef union _CFG {
struct {
unsigned char l;
unsigned char h;
} PART;
unsigned int val;
} CFG;

CFG config[TailleTab];
Tu accèderas par:
config[0].PART.l

ciao...
BruNews, MVP VC++
3
romain60112 Messages postés 70 Date d'inscription mercredi 1 octobre 2008 Statut Membre Dernière intervention 17 avril 2013 1
10 mai 2010 à 18:01
Avec mon compilo C j'ai un soucis :

typedef union _CFG {
struct {
unsigned char l;
unsigned char h;
}; // W1183C: invalid struct/union member declaration: name required
unsigned int val;
} CFG;

CFG config[TailleTab];
...
Mafonction()
{
config[0].l++; // E4138C: field `l' is undefined in `CFG': operator `.'
if(config[0].l == full_char) // E4138C: field `l' is undefined in `CFG': operator `.'

{ config[0].h++;
config[0].l=0;
if(config[0].h == full_char)
{ config[0].l=0;
config[0].h=0;
config[1].l++;
}
}
configuration[0] = config[0].val;
configuration[1] = config[1].val;
}

Comment résoudre le problème ?
Merci
Cordialement
Romain
0
Rejoignez-nous