How do I declare a (long) variable = 1234567

I have an 8 digit 7 segment display. I want to display numbers greater than 16 bits but can't. :frowning:

I've tried starting at....

long i = 1234567;
long i = 1234567L;
long i = 1234567LL;

but they all seem to result in modulii of 65K....

I have googled and looked at C++ Tutes that say the LL variant should work but I can't get it to.... :frowning:

I can start the counter at 12345 but when it gets to 65K it resets to 0 and starts again.

I thought "long" variables should be able to count to over 4 billion!?

This is probably not a problem for someone who has done this but it totally escapes me :frowning: :frowning: :frowning:

OBTW I put the number through

sprintf(buf, "%8u", i);

before sending the conversion to the display....

Could my problem be with sprintf?

const uint64_t variablename = 1234567;

uint64_t is an unsigned integer of 64 bits

const uint64_t bignumber = 1234567; // bignumber is variable's alias

uint64_t bignumber;

make bignumber = whatever you like (variable)

1234567 == 1 0010 1101 0110 1000 0111
uint32_t bignumber;
that would be a 32-bit unsigned integer

I recently read about this, I wish I could credit/attribute. the guy.

uintX_t -- u for unsigned, X = number of bits
intX_t -- signed, X = number of bits

You just have to type it right, which I seem to be having great difficulty with tonight (sorry for all the edits.)

You have to tell sprintf what you want to do exactly:

sprintf(buf, "%8lu", i);

See here for more parameters.

Thanks for your responses :slight_smile:

Runaway Pancake I tried both uint32_t AND uint64_t and they BOTH worked exactly like “long”. No improvement :frowning:

Thank you guix, your change to sprintf worked perfectly :smiley:

So it WAS sprintf not being supplied the correct conversion code.

Thanks a lot :smiley:

I am guilty of replying to the subject title instead of the body of the subject.
I think it's a good method, little used (just have to remember it right.) So, I'm trying to get in the habit of using it.
Have you memorized all of those (specifier characters, format specifiers, sub-specifiers, etc)? (-:

What code and/or libraries are you using for the display?
Maybe that's the culprit.

There is more than one way to get at a number's digits.
I like:

long mynumber = 1234567; // the number whose digits you want

// set up variables here
uint8_t ones, tens, hund, thou, myri, lakh, mill, cror;
uint32_t leftover;
// do the conversion here
cror = 0; mill = 0; lakh = 0; myri = 0;
thou = 0; hund = 0; tens = 0;
leftover = mynumber; 
while (leftover >= 10000000UL) { leftover -= 10000000UL; cror++; }
while (leftover >= 1000000UL)  { leftover -= 1000000UL; mill++; }
while (leftover >= 100000UL)   { leftover -= 100000UL; lakh++; }
while (leftover >= 10000UL)    { leftover -= 10000UL; myri++; }
while (leftover >= 1000UL)     { leftover -= 1000UL; thou++; }
while (leftover >= 100UL)      { leftover -= 100UL; hund++; }
while (leftover >= 10UL)       { leftover -= 10UL; tens++; }
ones = leftover;