By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,664 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,664 IT Pros & Developers. It's quick & easy.

Convert a very long int value To Hex

P: n/a
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

Aug 27 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Ahmad Jalil Qarshi wrote:
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.
Aug 27 '07 #2

P: n/a
On Aug 27, 9:05 pm, santosh <santosh....@gmail.comwrote:
Ahmad Jalil Qarshi wrote:
Hi,
I have an integer value which is very long like 998796744563877.
Now I
want to convert it into equivalent Hex value.
The result must be 8A9C63784361021F
I have used sprintf(pHex,"%0X",998796744563877). 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.
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

Aug 27 '07 #3

P: n/a
Ahmad Jalil Qarshi wrote:
On Aug 27, 9:05 pm, santosh <santosh....@gmail.comwrote:
>Ahmad Jalil Qarshi wrote:
Hi,
I have an integer value which is very long like 998796744563877.
Now I
want to convert it into equivalent Hex value.
The result must be 8A9C63784361021F
I have used sprintf(pHex,"%0X",998796744563877). 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.

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
$

Aug 27 '07 #4

P: n/a
Ahmad Jalil Qarshi wrote:
On Aug 27, 9:05 pm, santosh <santosh....@gmail.comwrote:
>Ahmad Jalil Qarshi wrote:
Hi,
I have an integer value which is very long like 998796744563877.
Now I
want to convert it into equivalent Hex value.
The result must be 8A9C63784361021F
I have used sprintf(pHex,"%0X",998796744563877). 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.

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?
Aug 27 '07 #5

P: n/a
santosh wrote:
Ahmad Jalil Qarshi wrote:
>On Aug 27, 9:05 pm, santosh <santosh....@gmail.comwrote:
>>Ahmad Jalil Qarshi wrote:
Hi,

I have an integer value which is very long like 998796744563877.
Now I
want to convert it into equivalent Hex value.

The result must be 8A9C63784361021F

I have used sprintf(pHex,"%0X",998796744563877). 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.

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
$
%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);
Aug 27 '07 #6

P: n/a
In article <11*********************@q4g2000prc.googlegroups.c om>,
Ahmad Jalil Qarshi <Ah****************@gmail.comwrote:
>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.)
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Aug 27 '07 #7

P: n/a
Harald van D?k wrote:
santosh wrote:
>Ahmad Jalil Qarshi wrote:
>>santosh wrote:
Ahmad Jalil Qarshi wrote:
Hi,

I have an integer value which is very long like 998796744563877.
Now I want to convert it into equivalent Hex value.
<snip>
>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);
Thanks. I should have known that. Also, I think, a cast of val to unsigned
long long would suffice, would it not?

Aug 27 '07 #8

P: n/a
santosh wrote:
Harald van D?k wrote:
>santosh wrote:
>>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);

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.
Aug 27 '07 #9

P: n/a

"Ahmad Jalil Qarshi" <Ah****************@gmail.comwrote in message
news:11*********************@q4g2000prc.googlegrou ps.com...
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[i]=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,

Regards,

Ahmad Jalil Qarshi

Aug 28 '07 #10

P: n/a
On Aug 28, 5:06 am, "cr88192" <cr88...@hotmail.comwrote:
"Ahmad Jalil Qarshi" <Ahmad.Jalil.Qar...@gmail.comwrote in messagenews:11*********************@q4g2000prc.goo glegroups.com...
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[i]=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,
Regards,
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

Aug 28 '07 #11

P: n/a
In article <11**********************@e9g2000prf.googlegroups. com>,
Ahmad Jalil Qarshi <Ah****************@gmail.comwrote:
>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.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Aug 28 '07 #12

P: n/a
Ahmad Jalil Qarshi <Ah****************@gmail.comwrites:
[...]
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'.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 28 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.