convert 64-integer to hex octet string

W

wenmang

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
 
P

Peter Nilsson

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.
 
K

Keith Thompson

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>.
 
C

CBFalconer

Peter said:
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.
 
T

Tor Rustad

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

santosh

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.
 
C

CBFalconer

santosh said:
Not necessarily. It's maximum value is yieled by the macro
ULLONG_MAX in limits.h.


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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top