I have an integer value which is very long like 9987967441778573855.
Now I
want to convert it into equivalent Hex value.
The result must be 8A9C63784361021F
I have used sprintf(pHex,"%0X",9987967441778573855). But it only
returns 8
Hex values.
When you have a literal constant that long, it is usually
not a good idea to count on the default integral promotions
to get the type right for you. On most systems,
0x8A9C63784361021F is too long to fit into an int, unsigned
int, long, or unsigned long.
If you are using C89, the list would stop there, and
0x8A9C63784361021F would undergo the standard unsigned long
reductions until the result fit into an unsigned long, commonly
(but not universally) resulting in 0x4361021F . But then you have
the problem that %0X is the format for an unsigned int, and would
need %0lX for unsigned long. And if you had a system with
an unusually large range for long, the value could end up as
of type long (signed) rather than unsigned long, in which case
it would have to be type cast to unsigned long before you could
use the %0lX format.
If you are using C99, the list would continue on to
long long and unsigned long long; 0x8A9C63784361021F would be
too big for long long on most (but not all) systems, but
would fit in unsigned long long on all systems that support
long long. But again it might happen to fit within signed long long
so it should be cast to unsigned long long before using a
%0llX format.
But the type you end up with becomes dependant upon the exact
ranges supported by the machine when you give such a large
constant without specifying a type modifier. It is better to be
certain, by coding something such as 9987967441778573855ULL
and then you *know* the type and no that it won't undergo any
unexpected value conversions (if it compiles at all.)