Page 1 of 1

chprintf for 64 bit values

Posted: Thu Aug 25, 2016 4:36 pm
by meatball
I have a GPS unit with that leaves me with ten digits to print out, so I've needed to use int64_t to store these coordinates.

My maths in the code are working with these values well, since debugging shows the variables turning out how they should be, but I'm having trouble printing the values to the serial terminal.

I always hate the ambiguity of "integer" and "long" as the chprintf description uses:

* The following parameter types (p) are supported:
* - <b>x</b> hexadecimal integer.
* - <b>X</b> hexadecimal long.
* - <b>o</b> octal integer.
* - <b>O</b> octal long.
* - <b>d</b> decimal signed integer.
* - <b>D</b> decimal signed long.
* - <b>u</b> decimal unsigned integer.
* - <b>U</b> decimal unsigned long.
* - <b>c</b> character.
* - <b>s</b> string.

Since chprintf() seems to be unable to printf 64 bit numbers, does that mean longs are 32 bit? This is not what I expected.

I assumed a long was 64 bits, so I've been calling

Code: Select all

volatile int64_t test1 = 4000000000;
volatile int64_t test2 = 4000000000;

chprintf((BaseSequentialStream *)&SD2, "%D,%D\r\n",test1, test2);

Notice how both variables are the same value, but the terminal output is:

Code: Select all

-294967296,0


Interesting, yes? I'm having trouble interpreting this output since neither of the variables are getting optimized out as far as I can tell. They are volatile. Is there a way I can print 64 bit ints?

Re: chprintf for 64 bit values

Posted: Thu Aug 25, 2016 5:58 pm
by Giovanni
Hi,

64 bits values are "long long" and currently not supported by chprintf, you could convert it in string and then print with %s.

Note that the support is not in chprintf because it would bring 64 bits math libraries and code size would increase, it is meant as a small utility not a full printf replacement.

Giovanni

Re: chprintf for 64 bit values

Posted: Thu Aug 25, 2016 6:29 pm
by meatball
Ah OK. Good to know then. I'll work on an itoa() or sprintf() implementation to handle the printing then.

I'll keep using chprintf() for the lightweight matter then.

Thanks!

Re: chprintf for 64 bit values

Posted: Fri Aug 26, 2016 4:32 pm
by rew
There are some long "strings" in GPS that only have numbers in them. You could interpret them as integers. But maybe a string is a better type?
Anyway, if you do go the "big integer" route... When printing, I think I would just chprintf ("%d%09d", num/1000000000LL, num % 1000000000LL); Be carful not to use the top 2-3 bits with this trick.....