2010-03-13 4 views
-2

나는 부호없는 문자 배열에 대한 질문이있어. 배열에 정수를 계속 저장할 수 있습니까?부호없는 문자 배열에 저장

예를 들어, 먼저 배열에 01011을 저장해야합니다. 그럼 내가 101을 저장할 필요가 어떻게 배열에 01011101로 저장할 수 있습니까? 도움을 주신 덕분에 !

+0

너무 막연 - 구체적인 예를 제공합니다 - 당신이 배열의 내용이 이후로 기대에 대해 설명합니다. 아마도 숙제 태그도 필요할까요? –

+0

배열의 길이가 유한합니다. 좀 더 자세히 설명해 주시겠습니까? – dirkgently

+0

01011과 0101101은 무엇입니까? 기지 8? (그렇다면 너무 큰 숫자로 부호없는 char에 저장 될 수 있습니다) base 2? 101? 기지 10? 그들은 캐릭터를 대표합니까? 자세한 내용을 알려주십시오. –

답변

0

먼저 01011을 저장하십시오. 값이 00001011이됩니다. 그러면 3 비트 더 저장할 때 세 위치 (왼쪽은 01011000)로 왼쪽 시프트를 수행하고 00000101을 OR로하면 01011101이됩니다. 그러나 이렇게하면 첫번째 임무를 마치고 5 비트 만 채웠다는 것을 확실히 알아야합니다.

0

분명히 커지면 배열의 크기를 조정해야합니다. 동적 메모리 할당/재 할당은 갈 수있는 방법입니다. 올바른 재 할당 전략을 선택하는 데주의하십시오.

그 외에도에서, 당신은 C.

0

당신은이 목적을 위해 bitstream라는 추상 데이터 유형을 작성해야 국한되지 않습니다 경우 C++ STL 컨테이너를보고 할 수 있습니다. 그것은 다음과 같은 인터페이스를 할 수 :

이 파일 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 

그런 다음이 인터페이스의 구현을 작성해야합니다. 파일은 bitstream.c이어야합니다. 이것은 훈련으로 남아 있습니다. 비트 스트림을 사용하려면

는 아주 간단해야한다 :

 
#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; 
}