2021年4月2日星期五

sprintf() function not giving the desired output

This is a small portion of code that splits variables into characters, all of the characters are then placed in sequence in a buffer. I have managed to get that part to work but I am having some issues with the sprintf function. The signed integer Cur_Scale is not outputting the correct characters.

I have tried modifying the formatting specifiers but always get a similar output. The only way I get the correct value is if I place a "-" in front of the argument / the variable.

I'm sure it's something very easy I'm overlooking but just can not spot it.

#include <stdio.h>  #include <stdlib.h>  #include <string.h>  #include <ctype.h>  #include <stdint.h>    int32_t Seconds_Timer = 123456789;  int32_t Cur_Scale = -254123678;  uint8_t buffer[250];  uint8_t buffer_2[20];    int main()  {                        memset(buffer, 0, sizeof buffer);       // Set all buffer to zero               memset(buffer_2, 0, sizeof buffer_2);   // Set all buffer_2 to zero               sprintf(buffer_2, "%ld", Seconds_Timer); // This will put up to 9 characters in buffer              //Split the variable into characters                            uint8_t a;              uint8_t b;                            for(a=0; a<=8; a++)                   {                  buffer[a]=buffer_2[a];                  }                            memset(buffer_2, 0, sizeof buffer_2);   // Set all buffer_2 to zero               sprintf(buffer_2, "%ld", Cur_Scale); // This will put up to 10 characters in buffer                            for(a=9,b=0; a<=18; a++,b++)                      {                  buffer[a]=buffer_2[b];                  }              printf(buffer, 20);     // Send confirmed value back in characters      }  
https://stackoverflow.com/questions/66926834/sprintf-function-not-giving-the-desired-output April 03, 2021 at 08:59AM

没有评论:

发表评论