sprintf does not give the expected result

Asked

Viewed 150 times

0

I have variables of the type Unsigned Long Int and would like to save its value in a string, to perform calculation of checksum and send via serial communication. void Payload(int long unsigned lastTime, float deltaOne, float deltaTwo, int factor){

[...]
sprintf(buffer, "CAPTION,%u,%s,%s,%d," lastTime,deltaOne,deltaTwo,factor);
SendWithCS(buffer);
}

But the converted value of int for string it’s not the same. Example:

int long unsigned record = 11285600;
float delta1 = 50.2035;
float delta2 = 54.2035
int factor = 5;
Payload(record,delta1,delta2,factor);

Exit:

CAPTION,22601,50.2035,54.2035,5,3F
  • Why is the first a pointer? Will you send this value or pointer? How can 5 data get out if you are only using 4? This code does not even compile, it has many errors.

  • ops, I typed wrong rs

  • There are other errors, see all. Read the whole comment above.

  • This code is part of another much larger code, but as the other parts are not relevancy just put a fragment of it, the last output value is given by the Sendwithcs; 54.2035,5,

  • Why should you show 22601 if what you’re going through is 11285600?

  • That’s the question, why are you showing 22601 if I’m passing 11285600?

  • Your question is confused, because this seemed to be what you expected. Actually I used your code, there is a lot of error in it, and it does not generate that no, certainly the code you are running is different from this posted.

  • This code will not compile, it depends on other functions, it is only possible to explain the part of sprintf , the only doubt is why I enter the variable in the function and the buffer string is presented with a value that differs from the original format.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

Show 4 more comments

1 answer

1

The code has several errors, after tidying them up what was left was the wrong formatting. View existing options.

This is a C code compiled with C++. These functions are avoided in idiomatic code C++.

The no-signal number was wrong, and the floating-point ones too.

#include <iostream>
using namespace std;

void Payload(unsigned long lastTime, float deltaOne, float deltaTwo, int factor) {
    char buffer[sizeof(unsigned long) + sizeof(float) + sizeof(float) + sizeof(int) + 13];
    sprintf(buffer, "CAPTION,%u,%.4f,%.4f,%d,", lastTime, deltaOne, deltaTwo, factor);
    printf("%s", buffer);
    printf("\nCAPTION,11285600,50.2035,54.2035,5,\n");
}

int main() {
    unsigned long record = 11285600UL;
    float delta1 = 50.2035;
    float delta2 = 54.2035;
    int factor = 5;
    Payload(record, delta1, delta2, factor);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.