Skip to main content
Share Your Experience: Take the 2024 Developer Survey
8 votes
Accepted

How to convert string to long?

Using atol(String.c_str()) looks OK to me. If there was a String.toLong() it would be written that way anyway. In fact, looking at the code for String.toInt() that's exactly what it does: long ...
Nick Gammon's user avatar
  • 38.3k
7 votes
Accepted

How to convert array of chars to array of ints?

If you have a C string containing ASCII numbers with a common delimiter (comma in this case) you can use the strtok() function to split it into individual strings. Then you can use atoi() to convert ...
Majenko's user avatar
  • 105k
7 votes

cast double to long leads to unexpected results

The problem is that float numbers are seldom an exact representation. So the float is stored as the closest number that is exactly representable as a float, namely 10446438 × 2−18 = 39....
Michel Keijzers's user avatar
6 votes

Convert String to IPAddress

The IPAddress class has a member function bool fromString(const char *address). const char *apipch = "192.168.4.1"; IPAddress apip; if (apip.fromString(apipch)) { // try to parse into the ...
6 votes

Converting from Float to Int yielding weird results. What am I doing wrong?

On an Arduino board with the 328 processor, an INT is stored as a 2 byte value, so the max number is 32,767. You want to use a LONG which is a 4 byte number which has a max of 2,147,483,647. When the ...
Chad G's user avatar
  • 630
5 votes
Accepted

String to unsigned long conversion

Arduino String class provides method c_str(). So you don't have to convert it to C string, as it's already stored as a C string internally. And as mentioned in comments, the second parameter of ...
KIIV's user avatar
  • 4,752
3 votes
Accepted

How to converte EPOCH time into time and date on Arduino?

Epoch are seconds from 1970-01-01. Get it from JSON as unsigned long. They are a number in the returned JSON (no ") from https://openweathermap.org/ doc: "sys":{"country":"JP","sunrise":1369769524,"...
Juraj's user avatar
  • 18k
3 votes
Accepted

Arduino String: use a non-null-terminated char array?

Assuming you are talking about the function bool painlessMesh::sendBroadcast( String &msg, bool includeSelf = false) Then you'll see that ultimately, the library passes msg into the function ...
Maximilian Gerhardt's user avatar
3 votes
Accepted

DS18B20 sending variable via Enthernet shield

Having re-read the question just before posting this answer, I find it odd that the DHT temp and humidity (also floats) work for you - how odd Use dtostrf char tStr[9] = { 0 }; // buffer to store ...
Jaromanda X's user avatar
3 votes

Problems on convert byte[] to String

If you change str += String(buffer[i]); to str += (char)buffer[i];, it will print the ascii characters to the serial monitor. The following sketch can be used to see the difference in binary compiled ...
VE7JRO's user avatar
  • 2,559
3 votes

Problems on convert byte[] to String

byte and char are the same. If you set 0 as string terminator after last character in the buffer, you get a zero terminated string. If you really must use String, you can create an instance with a ...
Juraj's user avatar
  • 18k
3 votes
Accepted

convert String to unsigned long long

avr-libc lacks strtoull() that I would normally use for such a conversion. You will have to do it manually. Fortunately it's quite simple - just start with y=0 and, for each character in the string, ...
Majenko's user avatar
  • 105k
3 votes
Accepted

How to understand the "power economy" of a voltage up-converter (boost converter)?

He is describing a boost converter which steps up voltage at the expense of current. To quote from the Wikipedia page on that subject: A boost converter (step-up converter) is a DC-to-DC power ...
Nick Gammon's user avatar
  • 38.3k
3 votes
Accepted

How to use a coma as the decimal separator?

Print the integer part, print a comma, then print the value minus the integer part. float value = 123.45; int ival = (int)value; int frac = (value - ival) * 100; Serial.print(ival); Serial.print(",")...
Majenko's user avatar
  • 105k
3 votes
Accepted

Converting array to char

C strings need to be NUL-terminated. char strcode[5]; ... ... ... strcode[4] = '\0'; ...
Ignacio Vazquez-Abrams's user avatar
3 votes
Accepted

Converting 16bit to float

You need to do two things: Right-shift the data to align it to the right of the variable, and Multiply it by the value of one bit. When right shifting be sure to do it in a signed data type so that ...
Majenko's user avatar
  • 105k
3 votes

Converting 16bit to float

Just a complement to Majenko's answer. The temperature register is set in such a way that you can interpret it in the same way irrespective of the selected resolution: int16_t rawval = part1 << ...
Edgar Bonet's user avatar
  • 43.3k
2 votes
Accepted

How to convert String to byte array

Why? And how do I receive desired output? getBytes docs The String member function getBytes() will null-terminate the string. You must add space for that. byte user1[17], user2[17], pass1[17], ...
Mikael Patel's user avatar
  • 7,969
2 votes

Create an a message from two inputs[Programming Help]

If you want help with optimisation then "Code Review" would be the better forum. Its very difficult to optimise code that's hard to read. You have used an inconsistent formatting style (which might ...
Code Gorilla's user avatar
  • 5,637
2 votes

Reconstruct uint32_t from char array to RTC.set

There are several possible issues here: can you send the number, as a string, from the PC to the ATtiny? can you convert the string to an actual number? can you give this number to the RTC? The ...
Edgar Bonet's user avatar
  • 43.3k
2 votes
Accepted

How to make I2C work on RS485?

I2C and RS485 are such vastly different protocols with such vastly different signalling needs that they cannot share the same cables. Further, I2C is not designed for connecting with long cables - it'...
Majenko's user avatar
  • 105k
2 votes
Accepted

How to convert char[12] to byte[6]

Please see the comments in the code: // SDMacAdress is zero terminated // (it should be because you use strcp not strncp) // char arr[12]; you need one extra byte for the zero char arr[13]; strcpy(...
Peter Paul Kiefer's user avatar
2 votes

How to convert char[12] to byte[6]

It is not clear why you are trying to implement it from scratch. If your input is always properly formatted you can perform the conversion in one call to sscanf char arr[] = "A4BDC334688C"; byte ...
AnT stands with Russia's user avatar
2 votes

Converting human readable date to a unix timestamp

After parsing the time into numbers, you can use the standard C time library time.h. More specifically, use the mktime function to convert a struct tm into a time_t, and use difftime to get the ...
Heinrich supports Monica's user avatar
2 votes
Accepted

Need function to convert bool array to int- why doesnt this work?

Assuming little-endian bools (least significant bool first), you should use simple bit-shifting, not floating point power calculations: for(int id=start_index;id<=stop_index;id++){ result |= ...
Majenko's user avatar
  • 105k
2 votes
Accepted

Convert character to Integer while leaving encoded

OK, so after the lengthy requests for clarifications, it appears that you are receiving a number in ASCII decimal ("12") and want to parse it into an integer. You may use the parseInt() ...
Edgar Bonet's user avatar
  • 43.3k
2 votes
Accepted

Float to int conversion not working right in method using varargs

With some inspiration from timemage's comment, I decided in the long run to implement my own power function that takes two int arguments and returns an int. Rather than use the pow function in the <...
DuluthIsSuperior's user avatar
2 votes
Accepted

How to convert serialized JSON string back to JSON object?

The mistake I did was thinking deserializeJson(datajson, datain) gives a Boolean as true for correct deserialization but accually it gives an object of DeserializationError err so need to check if ...
Avon97's user avatar
  • 161
2 votes

float miscalculation when int32_t variable is divided by 10

1139399025 is not a floating point number: the largest odd integer that can be exactly represented as a float is 224 − 1 = 16777215. Then, when var3 is cast to float, it gets rounded to the closest ...
Edgar Bonet's user avatar
  • 43.3k
2 votes

How do I use a transmitter and receiver to send string messages conditionally to a receiver?

I figured the issue out. code was a mess but after troubleshooting it for a while the if statements didn't make sense and had no good flow to them plus too complicated. I had copied and pasted some ...
Mario Ronci's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible