嘗試打印出存儲在數組中的每個字符的位。我查了一些代碼並嘗試了一個版本以適應我的需要。問題是我似乎只是獲取數組中的第一個字符。通過字符數組進行迭代,打印每個字符的位(在C中)
//read_buffer is the array I want to iterate through, bytes_to_read is the number of
//index positions I want to_read. (array is statically allocated and filled using read()
//funct, therefore there are some garbage bits after the char's I want), bytes_to_read
//is what's returned from read() and how many bytes were actually read into array
void PrintBits(char read_buffer[], int bytes_to_read)
{
int bit = 0;
int i = 0;
char char_to_print;
printf("bytes to read: %d\n", bytes_to_read); //DEBUG
for (; i < bytes_to_read; i++)
{
char_to_print = read_buffer[i];
for (; bit < 8; bit++)
{
printf("%i", char_to_print & 0X01);
char_to_print >> 1;
}
printf(" ");
printf("bytes_to_read: %d -- i: %d", bytes_to_read, i);
}
printf("\n");
}
基本上我得到的是:00000000不知道這是爲什麼。通過調試,我發現它只是打印第一位,沒有別的。我也證明了外層循環實際上是通過int的0 - 29迭代的...所以它應該遍歷數組中的char。我很難過。
*另外,有人可以告訴我什麼「& 0x01」正在做的printf語句。我發現在別人的代碼中,我不確定。大聲笑...也許這是我的問題?
真棒第二套眼睛,謝謝。我通常在C++中初始化我的循環中的所有內容,但使用C我一直在嘗試不同的東西,並沒有考慮到這些效果。謝謝。 – MCP 2012-04-28 19:19:48