0

I have a char array of length 10. It gets filled with an unpredictable number of chars, usually up to 6.

char charDummy[10] = "";

When i concatenate the char array with another, I get spaces, which are unwanted in later parsing.

char postStr[300] = "";
strcat(postStr, "&field1=");
strcat(postStr,dtostrf(rain05m,6,2,charDummy));

How do I trim a char array directly?

Workaround: loop thru the array and copy valid characters to a new char array with perfect number of rows. Due to memory constraints, I need to be parcimonious with the amount of variables created.

asked May 31, 2019 at 22:27

1 Answer 1

3

Your problem is that dtostrf() right-aligns the value in the space allotted to it. The simple answer is to tell it to left-align instead:

Conversion is done in the format "[-]d.ddd". The minimum field width of the output string (including the possible '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.

AVR LIBC Manual

So, just make your width negative and it will left align.

answered May 31, 2019 at 22:38
1
  • @tony gil: The string terminator (ASCII 0) has nothing to do with NULL and is never referred to as NULL. NULL is a completely different, unrelated thing in C and C++. Commented Jun 1, 2019 at 0:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.