How to use long long variable

All design tool related questions: compiler, assembler, linker. Embedded programming questions: assembler, C code.

Moderator: nferre

garypty
Posts: 9
Joined: Thu Sep 18, 2014 6:01 pm

How to use long long variable

Mon Nov 10, 2014 2:40 pm

I am using SAMD20J18 (M0+ MCU) and Atmel Studio 6.2. I have a hex number which consists of 5 bytes: 0xEEDDCCBBAA. This number is stored in an array: [0xEE 0xDD, 0xCC, 0xBB, 0xAA]. I want to convert it to 10base decimal number and represent it as a C-type string: "1025923398570". (0xEEDDCCBBAA is equal to 1,025,923,398,570). So I try to use the "unsigned long long" varible type:

Code: Select all

unsigned long long val = 0xEEDDCCBBAA;
unsigned char buf[50];
sprintf(buf, "%d", val);
However I find that I cannot assign the val to 0xEEDDCCBBAA.
How can I use the long long variable? Or how can I perform the task without using a long long variable?
Thank you.
jharley
Posts: 238
Joined: Thu Dec 06, 2012 6:40 am

Re: How to use long long variable

Mon Nov 10, 2014 7:25 pm

You are dealing with numbers that are too large for the intrinsic data types (largest is 64-bit).
garypty wrote:... how can I perform the task without using a long long variable?
One approach would be to use a library that can handle any size number, LibTomMath is one such library (is open source), even has a mp_toradix() function that can return the string representation of your "mp_int" value.

Note: "mp_int" (multiple precision integer) is the data type introduced in the library.

...or if this is a HW assignment you will have to write your own "to string" conversion function. :P

Regards.
jharley
Posts: 238
Joined: Thu Dec 06, 2012 6:40 am

Re: How to use long long variable

Mon Nov 10, 2014 10:31 pm

Well two problems...

One is the assignment, the second is how printf is invoked. (awneil has it)

For the assignment you will need a suffix

https://gcc.gnu.org/onlinedocs/gcc/Long-Long.html

(sorry for the tangent of the extended library, didn't drink my coffee and saw more bits than there actually were :lol: you don't need the library for this)

Return to “Development Tools”

Who is online

Users browsing this forum: No registered users and 3 guests