473,503 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert a very long int value To Hex

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
12 13361
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
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
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
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
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
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
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4958
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: ...
5
17984
by: Andrew V. Romero | last post by:
At work we have an excel file that contains the list of medications and their corresponding strengths. I would like to save the excel file as a text list and paste this list into a javascript...
4
3600
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
7
7073
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
17
4342
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
12
2006
by: Wolfgang Kaml | last post by:
Dear all, I am using the following code to retrieve the size of a certain file and the available (free) space on the disk. The problem is, that I get the size of the file returned as a Long and...
65
21320
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. ...
8
8388
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
5
3342
by: sonu | last post by:
hey good morning ...... how to convert a video file in .flv format in php for linux hosting......is there any package whis provide this facility . Can i use ffmpeg for linux hosting...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.