Convert a very long int value To Hex

A

Ahmad Jalil Qarshi

Hi,

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.

Kindly note that I am using pure C on Unix.

Thanks in anticipation,

Regards,

Ahmad Jalil Qarshi
 
S

santosh

Ahmad said:
Hi,

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.

Kindly note that I am using pure C on Unix.

Thanks in anticipation,

Try the "%Lx" or "%llx" format specifier.
 
A

Ahmad Jalil Qarshi

Try the "%Lx" or "%llx" format specifier.

Dear Santosh,

I have used "%Lx" and it returned 7d299ca5
When I used "%llx" it returned 7d299ca50000f0b2 which is not Hex for
998796744563877. It must return 38C667D299CA5.

Regards,

Ahmad Jalil Qarshi
 
S

santosh

Ahmad said:
Dear Santosh,

I have used "%Lx" and it returned 7d299ca5
When I used "%llx" it returned 7d299ca50000f0b2 which is not Hex for
998796744563877. It must return 38C667D299CA5.

Ensure that you are compiling in C99 conforming mode. The details to do that
will vary according to your compiler. For gcc pass the 'std=c99'
commandline switch.

Ensure that you use an object of type 'long long' or 'unsigned long long' to
store the value.

Ensure that your Standard library has support for C99 and the long long
type. Some broken libraries like Microsoft's default CRT do not have such
support.

The following program works fine on my system and prints the expected
values. Compare it with what you've written.

#include <stdio.h>

int main(void)
{
long long val = 998796744563877ll;
printf("%lld in hex is: %llx\n", val, val);
return 0;
}

$./t000
998796744563877 in hex is: 38c667d299ca5
$
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Ahmad said:
Dear Santosh,

I have used "%Lx" and it returned 7d299ca5
When I used "%llx" it returned 7d299ca50000f0b2 which is not Hex for
998796744563877. It must return 38C667D299CA5.

What santosh posted is correct C99, and while C99 is not widely implemented
in general, it appears from your result that your compiler and library do
support this, but there is a mistake in how you're using it. What exactly
does this program produce?

#include <stdio.h>
int main() {
unsigned long long ull = 998796744563877;
printf("%llx\n", ull);
}

If this doesn't produce 38c667d299ca5, the problem is with your compiler
and/or library. If this does produce 38c667d299ca5, and you cannot figure
out why it does here but not in your program, would you please post a
complete compilable example demonstrating the problem?
 
H

Harald van =?UTF-8?B?RMSzaw==?=

santosh said:
Ensure that you are compiling in C99 conforming mode. The details to do
that will vary according to your compiler. For gcc pass the 'std=c99'
commandline switch.

Ensure that you use an object of type 'long long' or 'unsigned long long'
to store the value.

Ensure that your Standard library has support for C99 and the long long
type. Some broken libraries like Microsoft's default CRT do not have such
support.

The following program works fine on my system and prints the expected
values. Compare it with what you've written.

#include <stdio.h>

int main(void)
{
long long val = 998796744563877ll;
printf("%lld in hex is: %llx\n", val, val);
return 0;
}

$./t000
998796744563877 in hex is: 38c667d299ca5
$

%lld prints signed long long values, %llx prints unsigned long long values.
Using %llx for signed values is not likely to cause errors, but strictly
speaking, it's not valid, and it can be trivially avoided here:

unsigned long long val = 998796744563877ll;
printf("%llu in hex is: %llx\n", val, val);
 
W

Walter Roberson

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.)
 
S

santosh

Harald said:
%lld prints signed long long values, %llx prints unsigned long long
values. Using %llx for signed values is not likely to cause errors, but
strictly speaking, it's not valid, and it can be trivially avoided here:

unsigned long long val = 998796744563877ll;
printf("%llu in hex is: %llx\n", val, val);

Thanks. I should have known that. Also, I think, a cast of val to unsigned
long long would suffice, would it not?
 
H

Harald van =?UTF-8?B?RMSzaw==?=

santosh said:
Thanks. I should have known that. Also, I think, a cast of val to unsigned
long long would suffice, would it not?

Indeed, that would work just as well here.
 
C

cr88192

Ahmad Jalil Qarshi said:
Hi,

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.

Kindly note that I am using pure C on Unix.

minor comments:
a number this size should really have 'LL' or 'ULL'.
9987967441778573855ULL (value too large for normal LL).

next up, as others have noted:
you need something like: '%llx'.

however, annoyingly, certain C libraries are broken (and do not support
'll').
in this particular case, you may be better off converting the value to a
string manually.

an example:
static char *hexchars="0123456789ABCDEF";

li=9987967441778573855ULL;
for(i=0; i<16; i++)pHex=hexchars[(li>>((15-i)*4))&0xF];
pHex[16]=0;


or, in a slightly more traditional style:
li=9987967441778573855ULL; i=16; t=pHex;
while(i--)*t++=hexchars[(li>>(i<<2))&0xF];
*t++=0;

(will state up front, ones' conventions are a matter of personal taste...).

or such...
 
A

Ahmad Jalil Qarshi

Ahmad Jalil Qarshi said:
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.
Kindly note that I am using pure C on Unix.

minor comments:
a number this size should really have 'LL' or 'ULL'.
9987967441778573855ULL (value too large for normal LL).

next up, as others have noted:
you need something like: '%llx'.

however, annoyingly, certain C libraries are broken (and do not support
'll').
in this particular case, you may be better off converting the value to a
string manually.

an example:
static char *hexchars="0123456789ABCDEF";

li=9987967441778573855ULL;
for(i=0; i<16; i++)pHex=hexchars[(li>>((15-i)*4))&0xF];
pHex[16]=0;

or, in a slightly more traditional style:
li=9987967441778573855ULL; i=16; t=pHex;
while(i--)*t++=hexchars[(li>>(i<<2))&0xF];
*t++=0;

(will state up front, ones' conventions are a matter of personal taste...).

or such...
Thanks in anticipation,

Ahmad Jalil Qarshi




Thanks all for your kind support. I have used the following code and
it worked fine.

unsigned long long val = 998796744563877ll;
printf("Hex is: %llX\n", val);

Thanks again.

Regards,
Ahmad Jalil Qarshi
 
W

Walter Roberson

Thanks all for your kind support. I have used the following code and
it worked fine.
unsigned long long val = 998796744563877ll;
printf("Hex is: %llX\n", val);

That's okay in that range of values, but the original
initializer you posted with had more digits, and using that pattern
could lead to problems with larger numbers. The 'll' modifier
on the literal is for -signed- numbers; if the literal exceeds
the range of -signed- long long then the value you get out
would be unspecified. If you use a 'ull' or 'ULL' modifier on
the literal, the behaviour would be well defined.
 
K

Keith Thompson

Ahmad Jalil Qarshi said:
Thanks all for your kind support. I have used the following code and
it worked fine.

unsigned long long val = 998796744563877ll;
printf("Hex is: %llX\n", val);

A suggestion: write it as 998796744563877LL. The lowercase letter 'l'
can be very hard to distinguish from the digit '1'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top