Print out uint64 to UART

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

I'm trying to simply print out a uint64 to UART. Currently I'm trying this: 

   

uint64 = 12345678;
char buff[10];
​sprintf(buff, “%” PRIu64, param);
UART_UartPurtString(buff);

   

Which all I get out from that is "lu" in the UART window.  I've tried other options I found on stack overflow but they all give the same output...Is there something I'm missing here? 

0 Likes
1 Solution
Anonymous
Not applicable

So this: 

   

sprintf(buff, "%ld", YourInt64);

   

Is working fine in PSoC 4 although you do get a warning of:

   
    

"format specifies type 'long' but the argument has type 'uint64'(aka 'unsigned long long'). 

   
   

Then if you try to change to this: 

   

sprintf(buff, "%lld", YourInt64);

   

All that prints out to UART is "ld".  It seems to working fine w/ @bobs answer 🙂 Thank you so much!!

View solution in original post

0 Likes
10 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Try the "l" (small letter "L") specifier, so you get  sprintf(buff, "%ld", YourInt64);

   

Have a look here for sprintf usage.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
ViDv_264506
Level 5
Level 5
50 sign-ins 25 sign-ins 5 solutions authored

You may try my old example, now updated to the last PSoC Creator version

   

Viktor

0 Likes
ViDv_264506
Level 5
Level 5
50 sign-ins 25 sign-ins 5 solutions authored

Bob, 

   

your idea .....

   

Try the "l" (small letter "L") specifier, so you get  sprintf(buff, "%ld", YourInt64);

   

is clear, but it does not work at least on PSoC Creator 3.3

   

Another modifier like "%llu" for uint64 does not work too.

   

Viktor

0 Likes
Anonymous
Not applicable

So this: 

   

sprintf(buff, "%ld", YourInt64);

   

Is working fine in PSoC 4 although you do get a warning of:

   
    

"format specifies type 'long' but the argument has type 'uint64'(aka 'unsigned long long'). 

   
   

Then if you try to change to this: 

   

sprintf(buff, "%lld", YourInt64);

   

All that prints out to UART is "ld".  It seems to working fine w/ @bobs answer 🙂 Thank you so much!!

0 Likes
ViDv_264506
Level 5
Level 5
50 sign-ins 25 sign-ins 5 solutions authored

Ran, 

   

on PSoC4 your code ....   sprintf(buff, "%ld", YourInt64); ..... "works" only if the YourInt64 value is < than uint32 

   

SPRINTF ERROR PERSISTS

   

Viktor 

0 Likes
Anonymous
Not applicable

Ah I see...that's really weird. Have you found a viable work around? 

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Oops, one of my posts disaipeared...  Or I forgot to send it  😞

   

I saw a trick using float formatting. Remember to set the heap size to at least 0x200 and allow newlib nano float formatting.

   

 

   

See attached.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
ViDv_264506
Level 5
Level 5
50 sign-ins 25 sign-ins 5 solutions authored

The uint64 -> string tested solution is attached.

   

Viktor

0 Likes
Anonymous
Not applicable

This works too

char *myUint64S(uint64 n) { // only one in a printf format string since local buffer
  static char res[21];
  int i = 21;
  res[--i] = 0;
  if(n == 0) res[--i] = '0';
  else while(n) {
    res[--i] = '0' + n%10;
    n /= 10;
  }
  return res + i;
}
0 Likes
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

user_459156114,

I 'had' the same issue as you today and just saw your post.  Sorry I'm late.

Bob's answer is to convert to float.  OK but is subject to rounding/truncating issues.

As I indicated, I had the same problem until I identified the reason that the proper 'C' syntax of "%lld" did not work the first time in sprintf().

Proper syntax:

sprintf(buff, "%lld", YourInt64);

However, you are probably using the default build settings of:

Use newlib-nano = True

Set it to False and "%lld" will work correctly with a UInt64.

pastedImage_1.png

Len
"Engineering is an Art. The Art of Compromise."
0 Likes