1

I am porting some C code for android . I am new to android. In my old code I have defined

typedef unsigned long uint64_t
typedef unsigned int      uint32_t;
#define A_HANDLE                0x0007LL
#define B_HANDLE                0x0009LL

C code is something

typedef uint64_t handle;

After some calculation handle value printed with %lu is 38654705665 and %016lx is 0000000900000001 .

But With Android I am not able to get these values with the same code. Even if I try to print the value of B_HANDLE I am not getting 0x0009. Can some one please help me how can i print and get the values forf uint64 on android

user503403
  • 259
  • 1
  • 2
  • 14
  • 1
    You need to show us the code that doesn't work. – user694733 Oct 11 '13 at 06:43
  • Since you for some reason don't adding "wrong" code example - my bet is that `sizeof(uint64_t)` of your manually defined uint64_t is actually 4 bytes on 32bit target android. – keltar Oct 11 '13 at 08:44
  • I am working on a 64 bit machine – user503403 Oct 11 '13 at 09:42
  • It's obvious that you do. But is your android system running on 64bit CPU too? If yes, what thing is it exactly? – keltar Oct 11 '13 at 09:51
  • I am using Win7 64 bit machine. When I tried to print using 0x%16xL it just print garbage value 0x 49ab7108L – user503403 Oct 14 '13 at 06:29
  • @user503403 what the hell?... You've asserted that you have correct results and your problem is only with android, what does it have to do with windows in any bit? – keltar Oct 14 '13 at 12:42

1 Answers1

2

You should never do something like this yourself:

typedef unsigned long uint64_t;
typedef unsigned int uint32_t;

These are reserved by C and including <stdint.h> should provide you the correct typedef for your platform.

Then you should use

#include <stdint.h>
#define A_HANDLE                UINT64_C(0x0007)
#define B_HANDLE                UINT64_C(0x0009)

to have the constants with the correct length specifier. (Your values had been signed, instead of unsigned, and long long instead of long.)

Print such values with the correct format specifiers by including a second standard header

#include <inttypes.h>
printf("valuue is %" PRIX64 "\n", handle);

or equivalent (PRIu64 for decimal unsigned).

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • I am porting an existing C code . So basically want to do minimal changes to that. – user503403 Oct 11 '13 at 07:15
  • 1
    That existing code is bad and, as you have experienced, not portable. I am indicating you *the* way that is foreseen by the C standard to avoid that kind of problems. – Jens Gustedt Oct 11 '13 at 07:33