473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

convert 64-integer to hex octet string

Hi all,

I have a confusion for representation of hex number:
For an unsigned long long number, its max value is:
unsigned long long maxNum = 18446744073709551615;
if I do:
printf("max uint64_t value in hex is %x\n", maxNum);
it prints as:
"max uint64_t value in hex is ffffffff"
but "ffffffff" is the max if I directly input it into MS calculator?
So, what is hex number for 18446744073709551615? it should be
0xffffffffffffffff, isn't it?
The next thing I like to do is to represent the max
number(18446744073709551615) as octet string, how can I do it?
I tried following but it seems not right:
unsigned long long maxNum=18446744073709551615;
unsigned char hexNumStr[sizeof(unsigned long long)];
memcpy(&hexNumStr, &maxNum, sizeof(unsigned long long));
how am I going to print out the content of hexNum as a string?
thx

Nov 16 '06 #1
7 19903
correction:
but "ffffffff" is NOT the max if I directly input it into MS calculator?
Nov 16 '06 #2
we*****@yahoo.com wrote:
Hi all,

I have a confusion for representation of hex number:
For an unsigned long long number, its max value is:
unsigned long long maxNum = 18446744073709551615;
No, the max value is...

unsigned long long maxNum = -1;

Or...

unsigned long long maxNum = ULLONG_MAX; /* from <limits.h*/

Which may be more than the number you posted.
if I do:
printf("max uint64_t value in hex is %x\n", maxNum);
%x takes an unsigned int. There is no guarantee that maxNum is
the same as an uint64_t.

If you want the max of an unsigned long long, you can do
something like...

printf("max unsigned long long = %llu\n", -1ull);
it prints as:
"max uint64_t value in hex is ffffffff"
That's because you lied to the compiler. [Your format string is a lie
to the user, but the C standard permits such things. ;-]

The printf function is a variadic function. You should look up the
problems with such functions. You should definitely look up
the specs for printf. Never use a function if you're only guessing
what the behaviour is going to be.

--
Peter

Nov 16 '06 #3
"we*****@yahoo.com" <we*****@yahoo.comwrites:
I have a confusion for representation of hex number:
For an unsigned long long number, its max value is:
unsigned long long maxNum = 18446744073709551615;
if I do:
printf("max uint64_t value in hex is %x\n", maxNum);
it prints as:
"max uint64_t value in hex is ffffffff"
[...]

The "%x" format expects an unsigned int. To print an unsigned long
use, "%llx" (assuming your implementation of printf() supports it).

But uint64_t (if it exists) isn't necessarily the same type as
unsigned long long. For that, you can use the PRIx64 macro (I think
that's right) in <inttypes.h>.

--
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.
Nov 16 '06 #4
Peter Nilsson wrote:
we*****@yahoo.com wrote:
>>
I have a confusion for representation of hex number:
For an unsigned long long number, its max value is:
unsigned long long maxNum = 18446744073709551615;

No, the max value is...

unsigned long long maxNum = -1;

Or...

unsigned long long maxNum = ULLONG_MAX; /* from <limits.h*/

Which may be more than the number you posted.
>if I do:
printf("max uint64_t value in hex is %x\n", maxNum);

%x takes an unsigned int. There is no guarantee that maxNum is
the same as an uint64_t.

If you want the max of an unsigned long long, you can do
something like...

printf("max unsigned long long = %llu\n", -1ull);
>it prints as:
"max uint64_t value in hex is ffffffff"

That's because you lied to the compiler. [Your format string is
a lie to the user, but the C standard permits such things. ;-]
However that %llu specifier will only work if your run time library
is C99 compliant (to at least some extent). The reason is that
printf is an interpreter for the format string, and if it doesn't
understand, it doesn't understand.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 17 '06 #5
we*****@yahoo.com wrote:
Hi all,

I have a confusion for representation of hex number:
For an unsigned long long number, its max value is:
unsigned long long maxNum = 18446744073709551615;
Ohh... I wouldn't remember that number by hart.
if I do:
printf("max uint64_t value in hex is %x\n", maxNum);
it prints as:
"max uint64_t value in hex is ffffffff"
%x format expect unsigned int
%lx format expect unsigned long
%llx format expect unsigned long long (C99)
but "ffffffff" is the max if I directly input it into MS calculator?
So, what is hex number for 18446744073709551615? it should be
0xffffffffffffffff, isn't it?
I don't have support for these C99 features myself...

#include <inttypes.h>
#include <stdio.h>

int main(void)
{
uint64_t maxNum = UINT64_MAX;

printf("max uint64_t value in hex is %016"
PRIx64 "\n", maxNum);

printf("max uint64_t value in octal is %"
PRIo64 "\n", maxNum);

return 0;
}

The next thing I like to do is to represent the max
number(18446744073709551615) as octet string, how can I do it?
Look at snprintf(...) and above.

--
Tor <torust AT online DOT no>

Nov 17 '06 #6
we*****@yahoo.com wrote:
Hi all,

I have a confusion for representation of hex number:
For an unsigned long long number, its max value is:
unsigned long long maxNum = 18446744073709551615;
Not necessarily. It's maximum value is yieled by the macro ULLONG_MAX
in limits.h.
if I do:
printf("max uint64_t value in hex is %x\n", maxNum);
it prints as:
"max uint64_t value in hex is ffffffff"
Thats because the 'x' format specifier expects an unsigned int
argument. So maxNum is automatically converted to a smaller type and
thus the value is truncated.

Also the correct format specifiers for the types given in stdint.h,
(like uint64_t), are given in inttypes.h. Here, maxNum is _not_ of type
uint64_t as you indicate in your format string. In some implementations
uint64_t may be a typedef for unsigned long long, but you cannot, and
should not, make that assumption portably. You should treat both as
different types.

To print an unsigned long long variable in hexadecimal format use 'llx'
as the format specifier.
but "ffffffff" is the max if I directly input it into MS calculator?
Irrelevant. Has to do with the limitations of MS calc.
So, what is hex number for 18446744073709551615? it should be
0xffffffffffffffff, isn't it?
The next thing I like to do is to represent the max
number(18446744073709551615) as octet string, how can I do it?
printf("maxNum in octal format = %llo\n", maxNum);
I tried following but it seems not right:
unsigned long long maxNum=18446744073709551615;
unsigned char hexNumStr[sizeof(unsigned long long)];
memcpy(&hexNumStr, &maxNum, sizeof(unsigned long long));
how am I going to print out the content of hexNum as a string?
Your above method is totally wrong. To print out, in string form, a
variable, use one of printf(), fprintf(), sprintf(), vsprintf() etc.

Nov 17 '06 #7
santosh wrote:
we*****@yahoo.com wrote:
>>
I have a confusion for representation of hex number:
For an unsigned long long number, its max value is:
unsigned long long maxNum = 18446744073709551615;

Not necessarily. It's maximum value is yieled by the macro
ULLONG_MAX in limits.h.
>if I do:
printf("max uint64_t value in hex is %x\n", maxNum);
it prints as:
"max uint64_t value in hex is ffffffff"

Thats because the 'x' format specifier expects an unsigned int
argument. So maxNum is automatically converted to a smaller type
and thus the value is truncated.
No it isn't. You have lied to the compiler, and invoked undefined
behaviour. It could simply cause all your toilets to overflow at
once.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 17 '06 #8

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

Similar topics

2
by: Vikrant | last post by:
Friends, I have read DB2/UDB 8.x 'RESTORE DATABASE Command', with my 'limited' knowledge & skill. I think it should address my concern, but I want advice/ opinion / experience and any care I...
4
by: techme | last post by:
I am surprised at how little info there is on this topic. We converted to DB2 v8.1.1.72 painlessly a few months ago. For specific reasons, we stayed on 32-bit. Our OS is AIX 5.2 and our hardward...
2
by: David A.Lethe | last post by:
I've got a console application that needs to be ported to the 64-bit windows native environment. I've downloaded the 64-bit build environment code from the MSDN site, but the docs say that I have...
0
by: joye | last post by:
Hello , I meet some data convertion problem, the sample code as following, namespace Configure { using namespace System ; typedef struct _SW{ char szVersion ;
3
by: Joseph Morales | last post by:
Due to Microsoft's ceasing to support Visual Studio 6.0, we are converting all our programs to be built in Visual Studio .NET. One of those programs is in VB 6.0. We can't convert it to VB .NET...
4
by: tommydkat | last post by:
Well, I've finally gotten UDB 8.2 FixPak 3 up and running on my HP-UX 11i system, thanks to Mr McBride and IBM support. :) I created a 32-bit instance and that's running just fine. However, I...
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
28
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am developing C++ COM native code (unmanaged C++) using Visual Studio 2005. I do not take any new features of 64-bit platform, and currently my code runs fine on 32-bit...
0
by: viren.chaudhary2008 | last post by:
I have just few questions. We are thinking of porting our 32 bit application to 64 bit as we need to use more memory usage and want to take advantage of 64 bit processor. Currently my applications...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.