簡體   English   中英

如何將int轉換為bit?

[英]How do I convert an int to bits?

這個網站的讀者很長一段時間,我不得不問的第一個問題是我在這里找不到答案; 也許我沒有使用正確的搜索詞,或者只是試圖以一種完全晦澀的方式來做= /

我正在使用Arduino和一些LED組成矩陣板,並試圖在其上顯示文本/符號。 為了實現這一點,我已經映射了一些數字(以0和1開頭),以及需要使用哪些像素來形成它們(每個像素為5高,寬3)。 我一直試圖將它們存儲在數組中,如下所示:

int displayLetters[2][4] = { // [2] = number of chars/digits contained in array, [4] = display width (3) + 1 blank column 
  {11111,10001,11111,00000}, // 0
  {01001,11111,00001,00000} // 1
};

然后,我創建了以下(我認為這很簡單)的函數,以根據需要將它們“分解”為0和1的數組,然后對其進行迭代以確定LED是應打開還是應熄滅。

void convertToBits(int inputValue, int outputArray[]) {
  int i = 0;

  if (inputValue == 0) { // set all values of array to zero
    for (i = 0; i < 5; i++) {
      outputArray[4-i] = 0;
    }
  } else {    
    while (inputValue > 0) {
      int digit = inputValue % 10;
      outputArray[4-i] = digit; // 4-i to prevent digits being stored in reverse order

      inputValue /= 10;
      i++;
    }
  }
}

期望對於displayLetters[1][0]我會得到一個像{0,1,0,0,1}的數組。 但是,當轉換為convertToBits它將顯示為513(我認為這是十六進制值嗎?)。

如果我把值只是100111111 (即一個工作正常的話), 10 (也能正常工作)分別為,那么它的行為預期。 我希望盡可能保持前導零以提高可讀性,但是如果不能那樣做,我想我可能會失去它們。

我玩過各種各樣的數據類型,字符串,uint8_t,unsigned int等,但我永遠無法使它滿足我的需要。

非常感謝您提供的任何幫助,我希望我已經提供了足夠的信息。 我認為我的代碼穩定並按預期運行,因此省略了其余代碼,這些問題是由convertToBits函數/傳遞給它的數據引起的。

非常感謝!

如果您使用的是C ++ 14,則可以使用以下形式的二進制文字

int displayLetters[2][4] = {
  {0b11111,0b10001,0b11111,0b00000}, // 0
  {0b01001,0b11111,0b00001,0b00000} // 1
};

否則,您必須使用十六進制文字:

int displayLetters[2][4] = {
  {0x1F,0x11,0x1F,0x00}, // 0
  {0x09,0x1F,0x01,0x00} // 1
};

為了獲得這5位長值的位,我們使用位運算符(很有趣,是嗎?):

outputArray[0] = (inputValue & 0x10) ? 1 : 0;
outputArray[1] = (inputValue & 0x08) ? 1 : 0;
outputArray[2] = (inputValue & 0x04) ? 1 : 0;
outputArray[3] = (inputValue & 0x02) ? 1 : 0;
outputArray[4] = (inputValue & 0x01) ? 1 : 0;

// Or with binary literals:

outputArray[0] = (inputValue & 0b10000) ? 1 : 0;
outputArray[1] = (inputValue &  0b1000) ? 1 : 0;
outputArray[2] = (inputValue &   0b100) ? 1 : 0;
outputArray[3] = (inputValue &    0b10) ? 1 : 0;
outputArray[4] = (inputValue &     0b1) ? 1 : 0;

那應該很好。 :)

編輯:添加轉換為類似布爾值。

以下是將正整數轉換為C ++中位的代碼

#include<iostream>
using namespace std;

void convertIntegerToBits(int number)  {
  if(number>0)  {
    convertIntegerToBits(number>>1);
     cout<<(number&1);
  }
  return ;
}

int main()  {
  convertIntegerToBits(1024);
  return 0;
}
vector<int> convert(int x) {
  vector<int> ret;
  while(x) {
   if (x&1)
     ret.push_back(1);
   else
     ret.push_back(0);
x>>=1;  
}
reverse(ret.begin(),ret.end());
return ret;
}

嘗試這個。

CC++如果以0開頭的數字,則編譯器會將其讀取為octal數字。 為避免這種情況,在聲明displayLetters數組時,請保留前導零。

void convertToBits(int inputValue, int outputArray[]) 
{
    int i = 0, width=5;
    int number = inputValue;
    for (i = 0; i < 5; i++) {
        if(inputValue>0){
            outputArray[4-i] = (inputValue%10);
            inputValue/=10;
        }
        else
            outputArray[4-i] = 0;
    }
}

此功能將注意leading zeros並且可以輕松更改bit序列的寬度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM