473,738 Members | 11,146 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 998796744177857 3855.
Now I
want to convert it into equivalent Hex value.

The result must be 8A9C63784361021 F

I have used sprintf(pHex,"% 0X",99879674417 78573855). 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 13500
Ahmad Jalil Qarshi wrote:
Hi,

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

The result must be 8A9C63784361021 F

I have used sprintf(pHex,"% 0X",99879674417 78573855). 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....@gm ail.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 8A9C63784361021 F
I have used sprintf(pHex,"% 0X",99879674456 3877). 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 7d299ca50000f0b 2 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....@gm ail.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 8A9C63784361021 F
I have used sprintf(pHex,"% 0X",99879674456 3877). 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 7d299ca50000f0b 2 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 = 998796744563877 ll;
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....@gm ail.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 8A9C63784361021 F
I have used sprintf(pHex,"% 0X",99879674456 3877). 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 7d299ca50000f0b 2 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....@gm ail.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 8A9C63784361021 F

I have used sprintf(pHex,"% 0X",99879674456 3877). 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 7d299ca50000f0b 2 which is not Hex for
99879674456387 7. 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 = 998796744563877 ll;
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 = 998796744563877 ll;
printf("%llu in hex is: %llx\n", val, val);
Aug 27 '07 #6
In article <11************ *********@q4g20 00prc.googlegro ups.com>,
Ahmad Jalil Qarshi <Ah************ ****@gmail.comw rote:
>I have an integer value which is very long like 998796744177857 3855.
Now I
want to convert it into equivalent Hex value.
>The result must be 8A9C63784361021 F
>I have used sprintf(pHex,"% 0X",99879674417 78573855). 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,
0x8A9C637843610 21F 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
0x8A9C637843610 21F 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; 0x8A9C637843610 21F 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 998796744177857 3855ULL
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 = 998796744563877 ll;
printf("%lld in hex is: %llx\n", val, val);
return 0;
}

$./t000
99879674456387 7 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 = 998796744563877 ll;
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 = 998796744563877 ll;
printf("%lld in hex is: %llx\n", val, val);
return 0;
}

$./t000
9987967445638 77 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 = 998796744563877 ll;
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.comw rote in message
news:11******** *************@q 4g2000prc.googl egroups.com...
Hi,

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

The result must be 8A9C63784361021 F

I have used sprintf(pHex,"% 0X",99879674417 78573855). 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'.
998796744177857 3855ULL (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="0123 456789ABCDEF";

li=998796744177 8573855ULL;
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=998796744177 8573855ULL; i=16; t=pHex;
while(i--)*t++=hexchars[(li>>(i<<2))&0x F];
*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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4973
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: FutureWarning: hex/oct constants > sys.maxint will return positive values in Python 2.4 and up It is because I used some constants such as 0x80ff3366, so I change it to 0x80ff3366L, hoping to get rid of the warning.
5
18025
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 function and have JS put this into an array. Then JS would use this array to create a selection list which displays only the names of the drugs. When the user selections one of the drugs, another selection list will be loaded with the avaiable...
4
3633
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 of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and Option along with a small helper function. In the code are comments describing my problem, look closely...
7
7118
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 without strtol or s/printf function. Thanks, whatluo.
17
4372
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 problem writing VB Double values to the file so as the Pascal program can read them as Pascal Real values. I've managed to find the algorithm to read the Pascal Real format and convert it to a VB Double, but I cannot figure out the opposite...
12
2032
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 the size of free disk space as UInt64. Apparently, there are no function to convert a Long to a UInt64 or to compare the two (I'm not expecting to convert the UInt64 to a Long necessarily, but it should work the other way round, right?). Short...
65
21449
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. #include <string.h> #include <stdio.h> /* Convert an ipv4 address to long integer */ /* "192.168.1.1" --> 3232235777 */ unsigned long iptol(char *ip){
8
8431
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 = "aabbccdd"; where aa is the LSB and dd is the MSB.
5
3363
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 project.......... plz help me.. thanks & regards Prabhat
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.