i have a string with 11 bits in it in binary and would like to make this into a decimal how can i do it the stuff in the string looks like this 10111100111 and when this is converted to a decimal should be 3023. the number in the binary will be changing and i need to make it into the decimal number to be able to do calculations with it
int readBinaryString(char *s) {
int result = 0;
while(*s) {
result <<= 1;
if(*s++ == '1') result |= 1;
}
return result;
}
void init() {
int value = readBinaryString("10111100111");
}
sorry im new to this programming a have the binary in a string and a can print it to serial monitor using Serial.println(data);
when i do this i get the binary out. the data part is a string how can i change this into a int
hi again how could i add bits to the string im using a libaray that outputs data as individule chars like this
if( vw_get_message(buf, &buflen) )
{
lcd.setCursor(0, 0);
lcd.print("Outdoor:");
for (i = 0; i < buflen; i++)
{
lcd.write(buf[i]);
}
lcd.setCursor(14, 0);
lcd.print((char)223);
Serial.print(value);
lcd.print("C");
}
that is to get it onto a lcd display but the problem is that i cannot use that number as a int when it is a char how can i use that code you posted to also get a number in an int format aswell as being able to print it to the lcd display
First, you will have to post your complete code, sending and receiving, and provide a bit of background information about your sensors, and information you are trying to send with your code,. and what you would like to do with it at the receiving side.
hi my code is at the moment going to just send a temperature reading to another arduino with the lcd that will display the temperature and also the temperature of its own location. i want the value sent that is going to the lcd to be an int so i can use it to turn things like and led on when it goes below a certain temperature and other things like this
I think there is a more simple way to go than using dtostrf to convert the temperature data to a string for transmission, and then converting it back into a float on the receive with atof. VirtualWire sends bytes, and you can cast the float into bytes (there are 4 in a float) for transmission with (uint8_t*)&myFloat and put them back together in the receiving sketch with memcpy of the received buffer into a float. Serial.print(), and lcd.print() can both handle floats to however many decimal places you desire with syntax like Serial.print(myFloat,2) for 2 dps. There is no need to present them with chars in order to print numbers. The casting to bytes, sending them, and reconstructing them with memcpy is useful for more complex data structures like arrays and structs.
Here's your Tx sketch modifed to eliminate the string conversion
cattledog:
I think there is a more simple way to go than using dtostrf to convert the temperature data to a string for transmission, and then converting it back into a float on the receive with atof. VirtualWire sends bytes, and you can cast the float into bytes (there are 4 in a float) for transmission with (uint8_t*)&myFloat and put them back together in the receiving sketch with memcpy of the received buffer into a float. Serial.print(), and lcd.print() can both handle floats to however many decimal places you desire with syntax like Serial.print(myFloat,2) for 2 dps. There is no need to present them with chars in order to print numbers. The casting to bytes, sending them, and reconstructing them with memcpy is useful for more complex data structures like arrays and structs.
I was having similar issues trying to transmit my temp sensor data via MQTT. And then, display that data via Home Assitant. On Home Assistant, the values are blank
I had help with getting to this point. It does EXACTLY what I want. It does display the temp data on serialPrint and MQTT. BUT, not Home Assistant. : (
So I started searching and found your post. Is the use of dtostrf "outdated"? Meaning: there is a better way? Maybe I need to tweak HA configuration file? Any Home Assistant users out there?