Bonjour,

Je vais faire simple, j'ai la structure suivante :
Code c : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
struct ffb_init0 {
    unsigned long long fileID; /* 48 bits */
    unsigned short clientID; /* 16 bits */
    unsigned long long blockNumbers; /* use only 45 bits */
    unsigned long packetNumbers; /* 32 bits */
    unsigned long long magicValue; /* 64 bits */
    unsigned long long fileHash[4]; /* at least 512 bits */
    unsigned char hashMethod; /* 8 bits */
    unsigned char protocolVersion; /* use only 4 bits */
    unsigned long long fileTime; /* use only 48 bits, hold unixtime */
    unsigned long long dataBlockPayload; /* 64 bits */
    unsigned char hasMetadata; /* 1 bit */
    unsigned char uft8Filename[69]; /* 552 bits for utf8 filename */
    /* 6 bits left to pad at 0x0 */
};

Je veux la convertir en tableau de bits, alors j'ai commencé comme suit :
Code c : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
    unsigned char barr_p[175];
 
    /* FileID */
    barr_p[0] = (p->fileID >> 40) & 0xff;
    barr_p[1] = (p->fileID >> 32) & 0xff;
    barr_p[2] = (p->fileID >> 24) & 0xff;
    barr_p[3] = (p->fileID >> 16) & 0xff;
    barr_p[4] = (p->fileID >> 8) & 0xff;
    barr_p[5] = p->fileID & 0xff;
 
    /* clientID */
    barr_p[6] = (p->clientID >> 8) & 0xff;
    barr_p[7] = p->clientID  & 0xff;
 
 
    /* blockNumbers */
    barr_p[8] = (p->blockNumbers >> 37) & 0xff;
    barr_p[9] = (p->blockNumbers >> 29) & 0xff
    barr_p[10] = (p->blockNumbers >> 21) & 0xff;
    barr_p[11] = (p->blockNumbers >> 13) & 0xff;
    barr_p[12] = (p->blockNumbers >> 5) & 0xff;
    /* Start to mix data in a byte as blockNumbers is 45 bits, so 3 bits are
     * used in the last byte and so we have to complete with the five bits of
     * the next value in the structure.
     */
    barr_p[13] = (((p->blockNumbers << 3) & 0xff) +
            ((p->packetNumbers >> 29) & 0xff)) & 0xff;
    barr_p[14] = (p->packetNumbers >> 21) & 0xff;
    barr_p[15] = (p->packetNumbers >> 13) & 0xff;
    barr_p[16] = (p->packetNumbers >> 5) & 0xff;

Le problèmes est bien le suivant, j'ai 175 cases de tableaux à remplir comme ça, avec un décalage (champs 45 bits), et je me demandais si quelqu'un a juste une idée pour automatiser un peu ça.