C Arduino将十进制转换为二进制再转换为十进制

C Arduino将十进制转换为二进制再转换为十进制,c,arduino,binary,decimal,pow,C,Arduino,Binary,Decimal,Pow,首先,我对Arduino的世界很陌生。虽然我认为我的代码相当简单,但它不起作用 因此,我正在为自己的知识编写一个脚本,将十进制值转换为8位二进制值。所以只有0到255个十进制数转换为二进制数,然后检查是否将其转换回十进制数 现在我很困惑,为什么我的代码看起来像是在工作,但直到三点才开始 如果有人能在这件事上提供帮助,我们将不胜感激 这是我的密码: void setup() { Serial.begin(9600); } void loop() { for(int i=0;i<

首先,我对Arduino的世界很陌生。虽然我认为我的代码相当简单,但它不起作用

因此,我正在为自己的知识编写一个脚本,将十进制值转换为8位二进制值。所以只有0到255个十进制数转换为二进制数,然后检查是否将其转换回十进制数

现在我很困惑,为什么我的代码看起来像是在工作,但直到三点才开始

如果有人能在这件事上提供帮助,我们将不胜感激

这是我的密码:

void setup() {
    Serial.begin(9600);
}

void loop() {
  for(int i=0;i<=255;i++) {
    Serial.print("DecToBin: ");
    Serial.print(i);
    Serial.print(" -> ");
    boolean Bin[] = {0,0,0,0,0,0,0,0};
    convertDecToBin(i,Bin);
    for(int j = 0;j<8;j++)
      Serial.print(Bin[j]);
    Serial.print(" -> ");
    Serial.print(convertBinToDec(Bin));
    Serial.print("\n");
  }
  delay(1000000);
  // Very long delay to emulate only one "Execution" of the loop()
}

/*
The following function convert any int from 0-255 to binary.
You need to pass the int as agrument.
You also need to pass the 8bit array of boolean
*/
void convertDecToBin(int Dec, boolean Bin[]) {
  for(int i = 7 ; i >= 0 ; i--) {
    if(pow(2, i)<=Dec) {
      Dec = Dec - pow(2, i);
      Bin[8-(i+1)] = 1;
    } else {
    }
  }
}

/*
This following function will convert any 8 bit array of boolean to a Decimal number.
you need to pass an boolean array of 8 bits
function return a int
*/
int convertBinToDec(boolean Bin[]) {
  int ReturnInt = 0;
  for(int i = 0;i<8;i++) {
    if(Bin[7-i]) {
      Serial.print("2^");
      Serial.print(i);
      ReturnInt = ReturnInt + (int)pow(2, i);
      Serial.print("=");
      Serial.print((int)pow(2, i));
      Serial.print("+");
    }
  }
  Serial.print(",");
  return ReturnInt;
}
pow()以双精度给出输出。照保罗的建议去做


如果arduino有math.h,您还可以尝试包含它,然后使用int(ceil(inta,intb))来获得精确的值。就我所知。。pow()以双精度形式给出输出,可能类似于3.999之类,默认情况下int在其中执行操作。。。所以它转换成3。ceil()将其带到下一个最大的int值。它可能会起作用。

因为您要处理0-7个1的数组,所以应该使用位移位:

int convertBinToDec(boolean Bin[]) {
  int ReturnInt = 0;
  for (int i = 0; i < 8; i++) {
    if (Bin[7 - i]) {
      Serial.print("Set Bit ");
      Serial.print(i);
      ReturnInt += 1<<i;
      Serial.print(" ==> ");
      Serial.print(1<<i);
      Serial.print(", ");
    }
  }
  return ReturnInt;
}

DecToBin: 1 -> 00000001 -> Set Bit 0 ==> 1, 1
DecToBin: 2 -> 00000010 -> Set Bit 1 ==> 1, 2
DecToBin: 3 -> 00000011 -> Set Bit 0 ==> 1, Set Bit 1 ==> 1, 3
DecToBin: 4 -> 00000100 -> Set Bit 2 ==> 2, 4
DecToBin: 5 -> 00000101 -> Set Bit 0 ==> 1, Set Bit 2 ==> 2, 5
DecToBin: 6 -> 00000110 -> Set Bit 1 ==> 1, Set Bit 2 ==> 2, 6
DecToBin: 7 -> 00000111 -> Set Bit 0 ==> 1, Set Bit 1 ==> 1, Set Bit 2 ==> 2, 7
DecToBin: 8 -> 00001000 -> Set Bit 3 ==> 4, 8
int convertBinToDec(布尔值Bin[]){
int ReturnInt=0;
对于(int i=0;i<8;i++){
如果(Bin[7-i]){
串行打印(“设置位”);
连载印刷(一);
ReturnInt+=1,1
DecToBin:2->00000010->设置位1==>1,2
DecToBin:3->00000011->设置位0==>1,设置位1==>1,3
DecToBin:4->00000100->设置位2==>2,4
DecToBin:5->00000101->设置位0==>1,设置位2==>2,5
DecToBin:6->00000110->设置位1==>1,设置位2==>2,6
DecToBin:7->00000111->设置位0==>1,设置位1==>1,设置位2=>2,7
DecToBin:8->00001000->设置位3==>4,8

对于具有精确整数结果的简单整数运算,不要使用浮点数学。而不要使用
pow(2,i)
使用
(1)从技术上讲,我所做的所有计算,虽然pow应该给出我验证过的孔号,并且pow的所有返回值都是.00。谢谢你的帮助!它是有效的!我还不了解位移位,但我对此进行了研究。但我仍然很困惑,为什么代码最初不能与pow一起工作。它可能会稍微快一点将
+=
转换为
|=
,因为这只是设置了一个位。我不确定-我会读数据表。没关系,它们都需要一个周期:zbw感谢您的研究。我在+=和|=之间犹豫了一下,但考虑到问题的入门级,我不想把问题弄得更糟:-)
int convertBinToDec(boolean Bin[]) {
  int ReturnInt = 0;
  for (int i = 0; i < 8; i++) {
    if (Bin[7 - i]) {
      Serial.print("Set Bit ");
      Serial.print(i);
      ReturnInt += 1<<i;
      Serial.print(" ==> ");
      Serial.print(1<<i);
      Serial.print(", ");
    }
  }
  return ReturnInt;
}

DecToBin: 1 -> 00000001 -> Set Bit 0 ==> 1, 1
DecToBin: 2 -> 00000010 -> Set Bit 1 ==> 1, 2
DecToBin: 3 -> 00000011 -> Set Bit 0 ==> 1, Set Bit 1 ==> 1, 3
DecToBin: 4 -> 00000100 -> Set Bit 2 ==> 2, 4
DecToBin: 5 -> 00000101 -> Set Bit 0 ==> 1, Set Bit 2 ==> 2, 5
DecToBin: 6 -> 00000110 -> Set Bit 1 ==> 1, Set Bit 2 ==> 2, 6
DecToBin: 7 -> 00000111 -> Set Bit 0 ==> 1, Set Bit 1 ==> 1, Set Bit 2 ==> 2, 7
DecToBin: 8 -> 00001000 -> Set Bit 3 ==> 4, 8