C 将十进制 scanf 输入转换为二进制数组

C Convert decimal scanf input to binary array

我正在开发一个程序,让我的 RaspPi 驱动旧的 HP 十六进制显示器。我目前正在使用 case/switch 选项来控制 GPIO 输出选择。我想使用直接二进制输入。如何将十进制 scanf() 输入转换为二进制数组?

    #include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>


int main()
{

 int displayNumber;

  wiringPiSetup();

  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);

  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(6, LOW);

    printf("Please select a number to display! 0-9\n");

    scanf("%i", &displayNumber);


   switch (displayNumber) {

    case 0:
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(6, HIGH);
    break;

    case 1:
    digitalWrite(6, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(6, HIGH);
    break;

    case 2:
    digitalWrite(6, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(6, HIGH);
    // code
    break;

    case 3:
    digitalWrite(6, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(0, HIGH);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(6, HIGH);
    // Code
    break;

    case 4:
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    digitalWrite(0, LOW);
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(6, HIGH);

    break;

    case 5:
    digitalWrite(6, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(0, LOW);
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(6, HIGH);

    break;

    case 6:
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    digitalWrite(0, HIGH);
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(6, HIGH);

    break;
    case 7:
    digitalWrite(6, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(0, HIGH);
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(6, HIGH);

    break;
    case 8:
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, HIGH);
    digitalWrite(6, HIGH);

    break;
    case 9:
    digitalWrite(6, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, HIGH);
    digitalWrite(6, LOW);
    break;
    }



return 0;

}

阅读这篇文章:

C Program to Convert Decimal to Binary

#include<stdio.h>
#include<conio.h>
int main()
{
  int d[20];
  int decimalno,i=0;
  printf("Enter the decimal number you want to conver to binary: \n");
  scanf("%d",&decimalno);
  while(decimalno>0)
  {
       d[i]=decimalno%2;
       i++;
       decimalno=decimalno/2;
  }
  printf("The binary version of the number you input, %d, is = ",decimalno);
  for(int j=i-1;j>=0;j--)
  {
        printf("%d",d[j]);
  }
  //getch();
  return 0;
}

我认为这是适合你的解决方案,不需要数组,因为你可以直接写位

int main()
{
    int displayNumber;
    int pins[6] = {3, 0, 1, 2};
    int i;

    wiringPiSetup();

    pinMode(0, OUTPUT);
    pinMode(1, OUTPUT);
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(6, OUTPUT);

    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(6, LOW);

    displayNumber = -1;
    while ((displayNumber < 0) || (displayNumber > 9))
    {
        printf("Please select a number to display! 0-9\n");
        scanf("%i", &displayNumber);
    }

    digitalWrite(6, LOW);
    for (i = 0 ; i < 4 ; i++)
        digitalWrite(pins[i], displayNumber & (1 << i) ? HIGH : LOW);
    digitalWrite(6, HIGH);

    return 0;
}

将 switch 语句替换为

digitalWrite(6, LOW);
digitalWrite(3, displayNumber & 1 ? HIGH : LOW);
digitalWrite(0, displayNumber & 2 ? HIGH : LOW);
digitalWrite(1, displayNumber & 4 ? HIGH : LOW);
digitalWrite(2, displayNumber & 8 ? HIGH : LOW);
digitalWrite(6, HIGH);