blob: eb257e5437ddfbb8f6dcdd65290d304357ded126 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
String inp = "01101000";
int decimalValue = 0;
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
Serial.println("");
Serial.println(inp);
String binary = inp.substring(2);
Serial.println(binary);
for (int i = 0; i < binary.length(); i++) {
// Calculate the value of the current bit
int bitValue = binary.charAt(i) - '0';
// Update the decimal value by shifting the bit to its correct position
decimalValue = (decimalValue << 1) | bitValue;
}
Serial.println(decimalValue);
Serial.println("");
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
}
|