开发者

storing in unsigned char array

i got a question about u开发者_如何学Pythonnsigned char array. How can i store an integer in the array continually?

for example, i need to store 01011 to the array first. Then i need to store 101, how can i stored as 01011101 in the array? thanks for your help!


Store 01011 first. You'll get value 00001011. Then when you want to store three more bits, perform a left shift by three positions (you'll get 01011000) and make OR with 00000101, you'll get 01011101. However, doing it this way you have to know definitely that you had only five bits filled after first assignment.


Obviously, you'll need to resize the array as it grows. Dynamic memory allocation/reallocation is a way to go, probably. Pay attention to choosing a right reallocation strategy.

Apart from that, you may want to look at C++ STL containers if you are not limited only to C.


You should write an abstract data type called bitstream for this purpose. It could have the following interface:

This is file 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

Then you need to write an implementation for this interface. It should be in a file called bitstream.c. This is left as an excercise.

To use the bitstream should then be pretty 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜