J'ai une question à propos de tableau non signé char. Comment est-ce que je peux stocker un nombre entier dans le tableau continuellement? Par exemple, j'ai besoin de stocker d'abord 01011 dans le tableau. Ensuite, j'ai besoin de stocker 101, comment puis-je stocker comme 01011101 dans le tableau? merci pour votre aide!stockage dans un tableau char non signé
Répondre
Stockez d'abord 01011. Vous obtiendrez la valeur 00001011. Ensuite, lorsque vous voulez stocker trois bits de plus, effectuez un décalage vers la gauche de trois positions (vous obtiendrez 01011000) et faites OU avec 00000101, vous obtiendrez 01011101. Cependant, le faire de cette façon vous Je dois absolument savoir que vous n'avez rempli que cinq bits après la première affectation.
De toute évidence, vous devrez redimensionner le tableau à mesure qu'il grandit. L'allocation/réallocation dynamique de la mémoire est une voie à suivre, probablement. Faites attention à choisir une bonne stratégie de réaffectation.
En dehors de cela, vous voudrez peut-être regarder C + les conteneurs STL si vous n'êtes pas limité seulement à C.
Vous devriez écrire un type de données abstrait appelé bitstream
à cet effet. Il pourrait avoir l'interface suivante:
Ce fichier est bitstream.h
:
#ifndef BITSTREAM_H #define BITSTREAM_H typedef struct bitstream_impl bitstream; struct bitstream_impl; /** * Creates a bit stream and allocates memory for it. Later, that memory * must be freed by calling bitstream_free(). */ extern bitstream *bitstream_new(); /** * Writes the lower 'bits' bits from the 'value' into the stream, starting with * the least significand bit ("little endian"). * * Returns nonzero if the writing was successful, and zero if the writing failed. */ extern int bitstream_writebits_le(bitstream *bs, unsigned int value, unsigned int bits); /** * Writes the lower 'bits' bits from the 'value' into the stream, starting with * the most significand bit ("big endian"). * * Returns nonzero if the writing was successful, and zero if the writing failed. */ extern int bitstream_writebits_be(bitstream *bs, unsigned int value, unsigned int bits); /** * Returns a pointer to the buffer of the bitstream. * * The returned pointer remains valid until the next time that one of the * bitstream_write* functions is called. The returned buffer must not be * modified. All bits in the buffer that have not yet been written are zero. * (This applies only to the last byte of the buffer.) Each byte of the buffer * contains at most 8 bits of data, even if CHAR_BITS is larger. */ extern unsigned char *bitstream_getbuffer(const bitstream *bs); /** * Returns the number of bits that have been written to the stream so far. */ extern unsigned int bitstream_getsize(const bitstream *bs); /** * Frees all the memory that is associated with this bitstream. */ extern void bitstream_free(bitstream *bs); #endif
Ensuite, vous devez écrire une implémentation de cette interface. Il devrait être dans un fichier appelé bitstream.c
. Ceci est laissé comme un exercice.
Pour utiliser le bitstream devrait alors être assez simple:
#include "bitstream.h" static void die(const char *msg) { perror(msg); exit(EXIT_FAILURE); } int main(void) { bitstream *bs; unsigned char *buf; bs = bitstream_new(); if (bs == NULL) die("bitstream_new"); if (!bitstream_writebits_be(bs, 0x000b, 5)) die("write 5 bits"); if (!bitstream_writebits_be(bs, 0x0005, 3)) die("write 3 bits"); if (bitstream_getsize(bs) != 8) die("FAIL: didn't write exactly 8 bits."); buf = bitstream_getbuffer(bs); if (buf[0] != 0x005dU) die("FAIL: didn't write the expected bits."); bitstream_free(bs); return 0; }
- 1. Char non signé char std :: vectorisé à unsigned char []?
- 2. comment récupérer 1 bit d'un tableau d'octets (tableau char non signé) dans C
- 3. Comment convertir une chaîne littérale à un tableau non signé char dans visual C++
- 4. Un compilateur qui prend 'char' comme 'non signé'?
- 5. Comment convertir dll .NET à un tableau non signé char pour l'utiliser de C++
- 6. Hexadécimal à un tableau non signé 8 bits dans VB.NET
- 7. Comportement défini de conversion de char * non signé en char * dans Objective-C
- 8. La meilleure façon de convertir char * non signé en int?
- 9. Comment déterminez-vous la longueur d'un char * non signé?
- 10. Signé ou non signé dans MySQL
- 11. C/C++ emballage signé char dans int
- 12. réinterpréter la conversion vers et à partir de char * et char non signé *
- 13. copier tableau non signé de géré à non géré
- 14. Comparer unsigned char = 0x00 et signé = char '00'
- 15. Affectation de la valeur char dans un tableau à la valeur char dans un autre tableau
- 16. coulée entre signé et non signé
- 17. C: Convertir signé à non signé
- 18. Comment convertir un System :: String en un caractère non signé *?
- 19. tableau de pointeurs vers un tableau char
- 20. Trier un tableau de char dans C
- 21. La différence entre signé et non signé dans MySQL?
- 22. comment initialiser un tableau char?
- 23. Redistribuer un tableau 2d char
- 24. C++ Conversion d'un flottant en un caractère non signé?
- 25. signé conversions non signées
- 26. Valeur maximale du caractère non signé
- 27. spawnv appel avec tableau de tableau de char (char * argv [])
- 28. Comment passer un entier comme paramètre non signé dans VB.NET?
- 29. Appliquer un thème Windows non signé dans WPF
- 30. Tableau de const char *
trop vague - expliquer ce que vous attendez le contenu du tableau à la suite - donner un exemple concret. Peut-être aussi besoin d'un tag de devoirs? –
Un tableau a une longueur finie. Pouvez-vous expliquer dans un peu plus de détails. – dirkgently
Quels sont 01011 et 0101101? base 8? (Si oui, ils sont trop gros pour être stockés dans un char non signé) base 2? et 101? base 10? Est-ce qu'ils représentent des personnages? S'il vous plaît donner plus de détails. –