Hello I did a simple code to convert float into char, and then the char again into float.
But when I'm adding a for loop to print all single Bytes inside the char array, all the code will not work anymore.
why would you attempt to go beyond the bounds of your array?
//for(int i = 0; i < sizeof(outstr)+1; i++) {
... Serial.print(outstr[ i ]);
I thought that maybe if I didn't put the +1 it won't print the last Byte of the array, anyway should I change it to i = sizeof(outstr) ?
So what do you see in the console??
if I include the the for loop, basically it will not go out of the loop, and it prints all type of ascii characters.
the strange thing is that if you remove all the part of the code after the for loop, all will work fine also with the small size of the outstr char array.
The last part of the code and the for loop are in conflict, I don't know why.
i've checked this example dtostrf().
The function dtostrf works well if I omit the part after the loop, so I think it' s not a problem of dtostrf. What I should I look for, or change in dtostrf? Thanks
I've changed the second argument WIDTH(is the number of character to use in the output)from 6 to 5 and now it works. But this was pure luck I would like to understand why. Here's my try:
I have a float with two decimals after the point, so in this case I have 5 Bytes.
2)If I set WIDTH = 6 there's no space for an end character such as '\0' or '\n', so if I change WIDTH to 5 and BUFFER(in my case outstr) to 6, I have 1 Byte more than WIDTH. If I not set this, it will happen something strange i can't explain in arduino memory.
TheMemberFormerlyKnownAsAWOL:
The second argument, and the size of the destination buffer
Not sure this is it, the doc described it as
The minimum field width of the output string (including the possible '.' and the possible sign for negative values) is given in width
So this seems more geared towards fitting data in a known space with left or right alignment, but if you have more data to print it might just overflow, esp as the doc states as well
The caller is responsible for providing sufficient storage in s.
Would be easy to test but I’m Not near an Arduino...
Coming back to this, I have a question. When I print the binary of a single Byte that Arduino sends to the Hardware serial port, I noticed that the binary are preceded by two ones --> 11.
While the point is equal to: 101110 that is correct.
The 2 would be equal to: 110010
3 = 110011
4 = 110100.... and so on.
Is this because they are numbers with a char format?
TheMemberFormerlyKnownAsAWOL:
The second argument, and the size of the destination buffer
So I did write a simple test and the second argument has nothing to do with constraining what gets written into the buffer but more how things get aligned / padded.
So You really need to size your buffer for the maximum width possible which is a mix of the number representation with the dot and sign at the expected precision and then the left or right padding if there is space.
riemanndiy:
Coming back to this, I have a question. When I print the binary of a single Byte that Arduino sends to the Hardware serial port, I noticed that the binary are preceded by two ones --> 11.
While the point is equal to: 101110 that is correct.
The 2 would be equal to: 110010
3 = 110011
4 = 110100.... and so on.
Is this because they are numbers with a char format?
I'm adding an image of the output I'm receiving
You are seeing the binary representation of the ASCII code for the characters.