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

时间:2015-01-01 16:50:41

标签: c raspberry-pi

我正在开发一个程序来让我的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;

}

3 个答案:

答案 0 :(得分:1)

替换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);

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}