首先,我对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;
}
现在注意3
值。这就是它开始被抵消的地方。据我所知,一切都是正确的。谁能发现错误?
DecToBin: 0 -> 00000000 -> ,0
DecToBin: 1 -> 00000001 -> 2^0=1+,1
DecToBin: 2 -> 00000010 -> 2^1=2+,2
DecToBin: 3 -> 00000011 -> 2^0=1+2^1=2+,3
DecToBin: 4 -> 00000100 -> 2^2=3+,3
DecToBin: 5 -> 00000101 -> 2^0=1+2^2=3+,4
DecToBin: 6 -> 00000110 -> 2^1=2+2^2=3+,5
DecToBin: 7 -> 00000111 -> 2^0=1+2^1=2+2^2=3+,6
DecToBin: 8 -> 00001000 -> 2^3=7+,7
DecToBin: 9 -> 00001001 -> 2^0=1+2^3=7+,8
DecToBin: 10 -> 00001010 -> 2^1=2+2^3=7+,9
DecToBin: 11 -> 00001011 -> 2^0=1+2^1=2+2^3=7+,10
DecToBin: 12 -> 00001100 -> 2^2=3+2^3=7+,10
DecToBin: 13 -> 00001101 -> 2^0=1+2^2=3+2^3=7+,11
DecToBin: 14 -> 00001110 -> 2^1=2+2^2=3+2^3=7+,12
DecToBin: 15 -> 00001111 -> 2^0=1+2^1=2+2^2=3+2^3=7+,13
DecToBin: 16 -> 00010000 -> 2^4=15+,15
DecToBin: 17 -> 00010001 -> 2^0=1+2^4=15+,16
DecToBin: 18 -> 00010010 -> 2^1=2+2^4=15+,17
DecToBin: 19 -> 00010011 -> 2^0=1+2^1=2+2^4=15+,18
DecToBin: 20 -> 00010100 -> 2^2=3+2^4=15+,18
DecToBin: 21 -> 00010101 -> 2^0=1+2^2=3+2^4=15+,19
DecToBin: 22 -> 00010110 -> 2^1=2+2^2=3+2^4=15+,20
DecToBin: 23 -> 00010111 -> 2^0=1+2^1=2+2^2=3+2^4=15+,21
DecToBin: 24 -> 00011000 -> 2^3=7+2^4=15+,22
DecToBin: 25 -> 00011001 -> 2^0=1+2^3=7+2^4=15+,23
DecToBin: 26 -> 00011010 -> 2^1=2+2^3=7+2^4=15+,24
DecToBin: 27 -> 00011011 -> 2^0=1+2^1=2+2^3=7+2^4=15+,25
DecToBin: 28 -> 00011100 -> 2^2=3+2^3=7+2^4=15+,25
DecToBin: 29 -> 00011101 -> 2^0=1+2^2=3+2^3=7+2^4=15+,26
DecToBin: 30 -> 00011110 -> 2^1=2+2^2=3+2^3=7+2^4=15+,27
DecToBin: 31 -> 00011111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^4=15+,28
DecToBin: 32 -> 00100000 -> 2^5=31+,31
DecToBin: 33 -> 00100001 -> 2^0=1+2^5=31+,32
DecToBin: 34 -> 00100010 -> 2^1=2+2^5=31+,33
DecToBin: 35 -> 00100011 -> 2^0=1+2^1=2+2^5=31+,34
DecToBin: 36 -> 00100100 -> 2^2=3+2^5=31+,34
DecToBin: 37 -> 00100101 -> 2^0=1+2^2=3+2^5=31+,35
DecToBin: 38 -> 00100110 -> 2^1=2+2^2=3+2^5=31+,36
DecToBin: 39 -> 00100111 -> 2^0=1+2^1=2+2^2=3+2^5=31+,37
DecToBin: 40 -> 00101000 -> 2^3=7+2^5=31+,38
DecToBin: 41 -> 00101001 -> 2^0=1+2^3=7+2^5=31+,39
DecToBin: 42 -> 00101010 -> 2^1=2+2^3=7+2^5=31+,40
DecToBin: 43 -> 00101011 -> 2^0=1+2^1=2+2^3=7+2^5=31+,41
DecToBin: 44 -> 00101100 -> 2^2=3+2^3=7+2^5=31+,41
DecToBin: 45 -> 00101101 -> 2^0=1+2^2=3+2^3=7+2^5=31+,42
DecToBin: 46 -> 00101110 -> 2^1=2+2^2=3+2^3=7+2^5=31+,43
DecToBin: 47 -> 00101111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^5=31+,44
DecToBin: 48 -> 00110000 -> 2^4=15+2^5=31+,46
DecToBin: 49 -> 00110001 -> 2^0=1+2^4=15+2^5=31+,47
DecToBin: 50 -> 00110010 -> 2^1=2+2^4=15+2^5=31+,48
DecToBin: 51 -> 00110011 -> 2^0=1+2^1=2+2^4=15+2^5=31+,49
DecToBin: 52 -> 00110100 -> 2^2=3+2^4=15+2^5=31+,49
DecToBin: 53 -> 00110101 -> 2^0=1+2^2=3+2^4=15+2^5=31+,50
DecToBin: 54 -> 00110110 -> 2^1=2+2^2=3+2^4=15+2^5=31+,51
DecToBin: 55 -> 00110111 -> 2^0=1+2^1=2+2^2=3+2^4=15+2^5=31+,52
DecToBin: 56 -> 00111000 -> 2^3=7+2^4=15+2^5=31+,53
DecToBin: 57 -> 00111001 -> 2^0=1+2^3=7+2^4=15+2^5=31+,54
DecToBin: 58 -> 00111010 -> 2^1=2+2^3=7+2^4=15+2^5=31+,55
DecToBin: 59 -> 00111011 -> 2^0=1+2^1=2+2^3=7+2^4=15+2^5=31+,56
DecToBin: 60 -> 00111100 -> 2^2=3+2^3=7+2^4=15+2^5=31+,56
DecToBin: 61 -> 00111101 -> 2^0=1+2^2=3+2^3=7+2^4=15+2^5=31+,57
DecToBin: 62 -> 00111110 -> 2^1=2+2^2=3+2^3=7+2^4=15+2^5=31+,58
DecToBin: 63 -> 00111111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^4=15+2^5=31+,59
DecToBin: 64 -> 01000000 -> 2^6=63+,63
DecToBin: 65 -> 01000001 -> 2^0=1+2^6=63+,64
DecToBin: 66 -> 01000010 -> 2^1=2+2^6=63+,65
DecToBin: 67 -> 01000011 -> 2^0=1+2^1=2+2^6=63+,66
DecToBin: 68 -> 01000100 -> 2^2=3+2^6=63+,66
DecToBin: 69 -> 01000101 -> 2^0=1+2^2=3+2^6=63+,67
DecToBin: 70 -> 01000110 -> 2^1=2+2^2=3+2^6=63+,68
DecToBin: 71 -> 01000111 -> 2^0=1+2^1=2+2^2=3+2^6=63+,69
DecToBin: 72 -> 01001000 -> 2^3=7+2^6=63+,70
DecToBin: 73 -> 01001001 -> 2^0=1+2^3=7+2^6=63+,71
DecToBin: 74 -> 01001010 -> 2^1=2+2^3=7+2^6=63+,72
DecToBin: 75 -> 01001011 -> 2^0=1+2^1=2+2^3=7+2^6=63+,73
DecToBin: 76 -> 01001100 -> 2^2=3+2^3=7+2^6=63+,73
DecToBin: 77 -> 01001101 -> 2^0=1+2^2=3+2^3=7+2^6=63+,74
DecToBin: 78 -> 01001110 -> 2^1=2+2^2=3+2^3=7+2^6=63+,75
DecToBin: 79 -> 01001111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^6=63+,76
DecToBin: 80 -> 01010000 -> 2^4=15+2^6=63+,78
DecToBin: 81 -> 01010001 -> 2^0=1+2^4=15+2^6=63+,79
DecToBin: 82 -> 01010010 -> 2^1=2+2^4=15+2^6=63+,80
DecToBin: 83 -> 01010011 -> 2^0=1+2^1=2+2^4=15+2^6=63+,81
DecToBin: 84 -> 01010100 -> 2^2=3+2^4=15+2^6=63+,81
DecToBin: 85 -> 01010101 -> 2^0=1+2^2=3+2^4=15+2^6=63+,82
DecToBin: 86 -> 01010110 -> 2^1=2+2^2=3+2^4=15+2^6=63+,83
DecToBin: 87 -> 01010111 -> 2^0=1+2^1=2+2^2=3+2^4=15+2^6=63+,84
DecToBin: 88 -> 01011000 -> 2^3=7+2^4=15+2^6=63+,85
DecToBin: 89 -> 01011001 -> 2^0=1+2^3=7+2^4=15+2^6=63+,86
DecToBin: 90 -> 01011010 -> 2^1=2+2^3=7+2^4=15+2^6=63+,87
DecToBin: 91 -> 01011011 -> 2^0=1+2^1=2+2^3=7+2^4=15+2^6=63+,88
DecToBin: 92 -> 01011100 -> 2^2=3+2^3=7+2^4=15+2^6=63+,88
DecToBin: 93 -> 01011101 -> 2^0=1+2^2=3+2^3=7+2^4=15+2^6=63+,89
DecToBin: 94 -> 01011110 -> 2^1=2+2^2=3+2^3=7+2^4=15+2^6=63+,90
DecToBin: 95 -> 01011111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^4=15+2^6=63+,91
DecToBin: 96 -> 01100000 -> 2^5=31+2^6=63+,94
DecToBin: 97 -> 01100001 -> 2^0=1+2^5=31+2^6=63+,95
DecToBin: 98 -> 01100010 -> 2^1=2+2^5=31+2^6=63+,96
DecToBin: 99 -> 01100011 -> 2^0=1+2^1=2+2^5=31+2^6=63+,97
DecToBin: 100 -> 01100100 -> 2^2=3+2^5=31+2^6=63+,97
DecToBin: 101 -> 01100101 -> 2^0=1+2^2=3+2^5=31+2^6=63+,98
DecToBin: 102 -> 01100110 -> 2^1=2+2^2=3+2^5=31+2^6=63+,99
DecToBin: 103 -> 01100111 -> 2^0=1+2^1=2+2^2=3+2^5=31+2^6=63+,100
DecToBin: 104 -> 01101000 -> 2^3=7+2^5=31+2^6=63+,101
DecToBin: 105 -> 01101001 -> 2^0=1+2^3=7+2^5=31+2^6=63+,102
DecToBin: 106 -> 01101010 -> 2^1=2+2^3=7+2^5=31+2^6=63+,103
DecToBin: 107 -> 01101011 -> 2^0=1+2^1=2+2^3=7+2^5=31+2^6=63+,104
DecToBin: 108 -> 01101100 -> 2^2=3+2^3=7+2^5=31+2^6=63+,104
DecToBin: 109 -> 01101101 -> 2^0=1+2^2=3+2^3=7+2^5=31+2^6=63+,105
DecToBin: 110 -> 01101110 -> 2^1=2+2^2=3+2^3=7+2^5=31+2^6=63+,106
DecToBin: 111 -> 01101111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^5=31+2^6=63+,107
DecToBin: 112 -> 01110000 -> 2^4=15+2^5=31+2^6=63+,109
DecToBin: 113 -> 01110001 -> 2^0=1+2^4=15+2^5=31+2^6=63+,110
DecToBin: 114 -> 01110010 -> 2^1=2+2^4=15+2^5=31+2^6=63+,111
DecToBin: 115 -> 01110011 -> 2^0=1+2^1=2+2^4=15+2^5=31+2^6=63+,112
DecToBin: 116 -> 01110100 -> 2^2=3+2^4=15+2^5=31+2^6=63+,112
DecToBin: 117 -> 01110101 -> 2^0=1+2^2=3+2^4=15+2^5=31+2^6=63+,113
DecToBin: 118 -> 01110110 -> 2^1=2+2^2=3+2^4=15+2^5=31+2^6=63+,114
DecToBin: 119 -> 01110111 -> 2^0=1+2^1=2+2^2=3+2^4=15+2^5=31+2^6=63+,115
DecToBin: 120 -> 01111000 -> 2^3=7+2^4=15+2^5=31+2^6=63+,116
DecToBin: 121 -> 01111001 -> 2^0=1+2^3=7+2^4=15+2^5=31+2^6=63+,117
DecToBin: 122 -> 01111010 -> 2^1=2+2^3=7+2^4=15+2^5=31+2^6=63+,118
DecToBin: 123 -> 01111011 -> 2^0=1+2^1=2+2^3=7+2^4=15+2^5=31+2^6=63+,119
DecToBin: 124 -> 01111100 -> 2^2=3+2^3=7+2^4=15+2^5=31+2^6=63+,119
DecToBin: 125 -> 01111101 -> 2^0=1+2^2=3+2^3=7+2^4=15+2^5=31+2^6=63+,120
DecToBin: 126 -> 01111110 -> 2^1=2+2^2=3+2^3=7+2^4=15+2^5=31+2^6=63+,121
DecToBin: 127 -> 01111111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^4=15+2^5=31+2^6=63+,122
DecToBin: 128 -> 10000000 -> 2^7=127+,127
DecToBin: 129 -> 10000001 -> 2^0=1+2^7=127+,128
DecToBin: 130 -> 10000010 -> 2^1=2+2^7=127+,129
DecToBin: 131 -> 10000011 -> 2^0=1+2^1=2+2^7=127+,130
DecToBin: 132 -> 10000100 -> 2^2=3+2^7=127+,130
DecToBin: 133 -> 10000101 -> 2^0=1+2^2=3+2^7=127+,131
DecToBin: 134 -> 10000110 -> 2^1=2+2^2=3+2^7=127+,132
DecToBin: 135 -> 10000111 -> 2^0=1+2^1=2+2^2=3+2^7=127+,133
DecToBin: 136 -> 10001000 -> 2^3=7+2^7=127+,134
DecToBin: 137 -> 10001001 -> 2^0=1+2^3=7+2^7=127+,135
DecToBin: 138 -> 10001010 -> 2^1=2+2^3=7+2^7=127+,136
DecToBin: 139 -> 10001011 -> 2^0=1+2^1=2+2^3=7+2^7=127+,137
DecToBin: 140 -> 10001100 -> 2^2=3+2^3=7+2^7=127+,137
DecToBin: 141 -> 10001101 -> 2^0=1+2^2=3+2^3=7+2^7=127+,138
DecToBin: 142 -> 10001110 -> 2^1=2+2^2=3+2^3=7+2^7=127+,139
DecToBin: 143 -> 10001111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^7=127+,140
DecToBin: 144 -> 10010000 -> 2^4=15+2^7=127+,142
DecToBin: 145 -> 10010001 -> 2^0=1+2^4=15+2^7=127+,143
DecToBin: 146 -> 10010010 -> 2^1=2+2^4=15+2^7=127+,144
DecToBin: 147 -> 10010011 -> 2^0=1+2^1=2+2^4=15+2^7=127+,145
DecToBin: 148 -> 10010100 -> 2^2=3+2^4=15+2^7=127+,145
DecToBin: 149 -> 10010101 -> 2^0=1+2^2=3+2^4=15+2^7=127+,146
DecToBin: 150 -> 10010110 -> 2^1=2+2^2=3+2^4=15+2^7=127+,147
DecToBin: 151 -> 10010111 -> 2^0=1+2^1=2+2^2=3+2^4=15+2^7=127+,148
DecToBin: 152 -> 10011000 -> 2^3=7+2^4=15+2^7=127+,149
DecToBin: 153 -> 10011001 -> 2^0=1+2^3=7+2^4=15+2^7=127+,150
DecToBin: 154 -> 10011010 -> 2^1=2+2^3=7+2^4=15+2^7=127+,151
DecToBin: 155 -> 10011011 -> 2^0=1+2^1=2+2^3=7+2^4=15+2^7=127+,152
DecToBin: 156 -> 10011100 -> 2^2=3+2^3=7+2^4=15+2^7=127+,152
DecToBin: 157 -> 10011101 -> 2^0=1+2^2=3+2^3=7+2^4=15+2^7=127+,153
DecToBin: 158 -> 10011110 -> 2^1=2+2^2=3+2^3=7+2^4=15+2^7=127+,154
DecToBin: 159 -> 10011111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^4=15+2^7=127+,155
DecToBin: 160 -> 10100000 -> 2^5=31+2^7=127+,158
DecToBin: 161 -> 10100001 -> 2^0=1+2^5=31+2^7=127+,159
DecToBin: 162 -> 10100010 -> 2^1=2+2^5=31+2^7=127+,160
DecToBin: 163 -> 10100011 -> 2^0=1+2^1=2+2^5=31+2^7=127+,161
DecToBin: 164 -> 10100100 -> 2^2=3+2^5=31+2^7=127+,161
DecToBin: 165 -> 10100101 -> 2^0=1+2^2=3+2^5=31+2^7=127+,162
DecToBin: 166 -> 10100110 -> 2^1=2+2^2=3+2^5=31+2^7=127+,163
DecToBin: 167 -> 10100111 -> 2^0=1+2^1=2+2^2=3+2^5=31+2^7=127+,164
DecToBin: 168 -> 10101000 -> 2^3=7+2^5=31+2^7=127+,165
DecToBin: 169 -> 10101001 -> 2^0=1+2^3=7+2^5=31+2^7=127+,166
DecToBin: 170 -> 10101010 -> 2^1=2+2^3=7+2^5=31+2^7=127+,167
DecToBin: 171 -> 10101011 -> 2^0=1+2^1=2+2^3=7+2^5=31+2^7=127+,168
DecToBin: 172 -> 10101100 -> 2^2=3+2^3=7+2^5=31+2^7=127+,168
DecToBin: 173 -> 10101101 -> 2^0=1+2^2=3+2^3=7+2^5=31+2^7=127+,169
DecToBin: 174 -> 10101110 -> 2^1=2+2^2=3+2^3=7+2^5=31+2^7=127+,170
DecToBin: 175 -> 10101111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^5=31+2^7=127+,171
DecToBin: 176 -> 10110000 -> 2^4=15+2^5=31+2^7=127+,173
DecToBin: 177 -> 10110001 -> 2^0=1+2^4=15+2^5=31+2^7=127+,174
DecToBin: 178 -> 10110010 -> 2^1=2+2^4=15+2^5=31+2^7=127+,175
DecToBin: 179 -> 10110011 -> 2^0=1+2^1=2+2^4=15+2^5=31+2^7=127+,176
DecToBin: 180 -> 10110100 -> 2^2=3+2^4=15+2^5=31+2^7=127+,176
DecToBin: 181 -> 10110101 -> 2^0=1+2^2=3+2^4=15+2^5=31+2^7=127+,177
DecToBin: 182 -> 10110110 -> 2^1=2+2^2=3+2^4=15+2^5=31+2^7=127+,178
DecToBin: 183 -> 10110111 -> 2^0=1+2^1=2+2^2=3+2^4=15+2^5=31+2^7=127+,179
DecToBin: 184 -> 10111000 -> 2^3=7+2^4=15+2^5=31+2^7=127+,180
DecToBin: 185 -> 10111001 -> 2^0=1+2^3=7+2^4=15+2^5=31+2^7=127+,181
DecToBin: 186 -> 10111010 -> 2^1=2+2^3=7+2^4=15+2^5=31+2^7=127+,182
DecToBin: 187 -> 10111011 -> 2^0=1+2^1=2+2^3=7+2^4=15+2^5=31+2^7=127+,183
DecToBin: 188 -> 10111100 -> 2^2=3+2^3=7+2^4=15+2^5=31+2^7=127+,183
DecToBin: 189 -> 10111101 -> 2^0=1+2^2=3+2^3=7+2^4=15+2^5=31+2^7=127+,184
DecToBin: 190 -> 10111110 -> 2^1=2+2^2=3+2^3=7+2^4=15+2^5=31+2^7=127+,185
DecToBin: 191 -> 10111111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^4=15+2^5=31+2^7=127+,186
DecToBin: 192 -> 11000000 -> 2^6=63+2^7=127+,190
DecToBin: 193 -> 11000001 -> 2^0=1+2^6=63+2^7=127+,191
DecToBin: 194 -> 11000010 -> 2^1=2+2^6=63+2^7=127+,192
DecToBin: 195 -> 11000011 -> 2^0=1+2^1=2+2^6=63+2^7=127+,193
DecToBin: 196 -> 11000100 -> 2^2=3+2^6=63+2^7=127+,193
DecToBin: 197 -> 11000101 -> 2^0=1+2^2=3+2^6=63+2^7=127+,194
DecToBin: 198 -> 11000110 -> 2^1=2+2^2=3+2^6=63+2^7=127+,195
DecToBin: 199 -> 11000111 -> 2^0=1+2^1=2+2^2=3+2^6=63+2^7=127+,196
DecToBin: 200 -> 11001000 -> 2^3=7+2^6=63+2^7=127+,197
DecToBin: 201 -> 11001001 -> 2^0=1+2^3=7+2^6=63+2^7=127+,198
DecToBin: 202 -> 11001010 -> 2^1=2+2^3=7+2^6=63+2^7=127+,199
DecToBin: 203 -> 11001011 -> 2^0=1+2^1=2+2^3=7+2^6=63+2^7=127+,200
DecToBin: 204 -> 11001100 -> 2^2=3+2^3=7+2^6=63+2^7=127+,200
DecToBin: 205 -> 11001101 -> 2^0=1+2^2=3+2^3=7+2^6=63+2^7=127+,201
DecToBin: 206 -> 11001110 -> 2^1=2+2^2=3+2^3=7+2^6=63+2^7=127+,202
DecToBin: 207 -> 11001111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^6=63+2^7=127+,203
DecToBin: 208 -> 11010000 -> 2^4=15+2^6=63+2^7=127+,205
DecToBin: 209 -> 11010001 -> 2^0=1+2^4=15+2^6=63+2^7=127+,206
DecToBin: 210 -> 11010010 -> 2^1=2+2^4=15+2^6=63+2^7=127+,207
DecToBin: 211 -> 11010011 -> 2^0=1+2^1=2+2^4=15+2^6=63+2^7=127+,208
DecToBin: 212 -> 11010100 -> 2^2=3+2^4=15+2^6=63+2^7=127+,208
DecToBin: 213 -> 11010101 -> 2^0=1+2^2=3+2^4=15+2^6=63+2^7=127+,209
DecToBin: 214 -> 11010110 -> 2^1=2+2^2=3+2^4=15+2^6=63+2^7=127+,210
DecToBin: 215 -> 11010111 -> 2^0=1+2^1=2+2^2=3+2^4=15+2^6=63+2^7=127+,211
DecToBin: 216 -> 11011000 -> 2^3=7+2^4=15+2^6=63+2^7=127+,212
DecToBin: 217 -> 11011001 -> 2^0=1+2^3=7+2^4=15+2^6=63+2^7=127+,213
DecToBin: 218 -> 11011010 -> 2^1=2+2^3=7+2^4=15+2^6=63+2^7=127+,214
DecToBin: 219 -> 11011011 -> 2^0=1+2^1=2+2^3=7+2^4=15+2^6=63+2^7=127+,215
DecToBin: 220 -> 11011100 -> 2^2=3+2^3=7+2^4=15+2^6=63+2^7=127+,215
DecToBin: 221 -> 11011101 -> 2^0=1+2^2=3+2^3=7+2^4=15+2^6=63+2^7=127+,216
DecToBin: 222 -> 11011110 -> 2^1=2+2^2=3+2^3=7+2^4=15+2^6=63+2^7=127+,217
DecToBin: 223 -> 11011111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^4=15+2^6=63+2^7=127+,218
DecToBin: 224 -> 11100000 -> 2^5=31+2^6=63+2^7=127+,221
DecToBin: 225 -> 11100001 -> 2^0=1+2^5=31+2^6=63+2^7=127+,222
DecToBin: 226 -> 11100010 -> 2^1=2+2^5=31+2^6=63+2^7=127+,223
DecToBin: 227 -> 11100011 -> 2^0=1+2^1=2+2^5=31+2^6=63+2^7=127+,224
DecToBin: 228 -> 11100100 -> 2^2=3+2^5=31+2^6=63+2^7=127+,224
DecToBin: 229 -> 11100101 -> 2^0=1+2^2=3+2^5=31+2^6=63+2^7=127+,225
DecToBin: 230 -> 11100110 -> 2^1=2+2^2=3+2^5=31+2^6=63+2^7=127+,226
DecToBin: 231 -> 11100111 -> 2^0=1+2^1=2+2^2=3+2^5=31+2^6=63+2^7=127+,227
DecToBin: 232 -> 11101000 -> 2^3=7+2^5=31+2^6=63+2^7=127+,228
DecToBin: 233 -> 11101001 -> 2^0=1+2^3=7+2^5=31+2^6=63+2^7=127+,229
DecToBin: 234 -> 11101010 -> 2^1=2+2^3=7+2^5=31+2^6=63+2^7=127+,230
DecToBin: 235 -> 11101011 -> 2^0=1+2^1=2+2^3=7+2^5=31+2^6=63+2^7=127+,231
DecToBin: 236 -> 11101100 -> 2^2=3+2^3=7+2^5=31+2^6=63+2^7=127+,231
DecToBin: 237 -> 11101101 -> 2^0=1+2^2=3+2^3=7+2^5=31+2^6=63+2^7=127+,232
DecToBin: 238 -> 11101110 -> 2^1=2+2^2=3+2^3=7+2^5=31+2^6=63+2^7=127+,233
DecToBin: 239 -> 11101111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^5=31+2^6=63+2^7=127+,234
DecToBin: 240 -> 11110000 -> 2^4=15+2^5=31+2^6=63+2^7=127+,236
DecToBin: 241 -> 11110001 -> 2^0=1+2^4=15+2^5=31+2^6=63+2^7=127+,237
DecToBin: 242 -> 11110010 -> 2^1=2+2^4=15+2^5=31+2^6=63+2^7=127+,238
DecToBin: 243 -> 11110011 -> 2^0=1+2^1=2+2^4=15+2^5=31+2^6=63+2^7=127+,239
DecToBin: 244 -> 11110100 -> 2^2=3+2^4=15+2^5=31+2^6=63+2^7=127+,239
DecToBin: 245 -> 11110101 -> 2^0=1+2^2=3+2^4=15+2^5=31+2^6=63+2^7=127+,240
DecToBin: 246 -> 11110110 -> 2^1=2+2^2=3+2^4=15+2^5=31+2^6=63+2^7=127+,241
DecToBin: 247 -> 11110111 -> 2^0=1+2^1=2+2^2=3+2^4=15+2^5=31+2^6=63+2^7=127+,242
DecToBin: 248 -> 11111000 -> 2^3=7+2^4=15+2^5=31+2^6=63+2^7=127+,243
DecToBin: 249 -> 11111001 -> 2^0=1+2^3=7+2^4=15+2^5=31+2^6=63+2^7=127+,244
DecToBin: 250 -> 11111010 -> 2^1=2+2^3=7+2^4=15+2^5=31+2^6=63+2^7=127+,245
DecToBin: 251 -> 11111011 -> 2^0=1+2^1=2+2^3=7+2^4=15+2^5=31+2^6=63+2^7=127+,246
DecToBin: 252 -> 11111100 -> 2^2=3+2^3=7+2^4=15+2^5=31+2^6=63+2^7=127+,246
DecToBin: 253 -> 11111101 -> 2^0=1+2^2=3+2^3=7+2^4=15+2^5=31+2^6=63+2^7=127+,247
DecToBin: 254 -> 11111110 -> 2^1=2+2^2=3+2^3=7+2^4=15+2^5=31+2^6=63+2^7=127+,248
DecToBin: 255 -> 11111111 -> 2^0=1+2^1=2+2^2=3+2^3=7+2^4=15+2^5=31+2^6=63+2^7=127+,249
答案 0 :(得分:2)
由于你要经历一个0到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
答案 1 :(得分:1)
pow()给出输出为double。像保罗建议的那样做。
如果arduino拥有它,你也可以尝试包括math.h然后使用int(ceil(pow(int a,int b)))来获得确切的值。据我所知.. pow()给出的输出为双倍,可能像3.999的东西,默认情况下int在其中做地板...所以它转换为3. ceil()将它带到下一个最大的int值。它可能有用。