# convert 64-integer to hex octet string

Discussion in 'C Programming' started by wenmang@yahoo.com, Nov 16, 2006.

1. ### Guest

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, 2006

2. ### Guest

correction:
> but "ffffffff" is NOT the max if I directly input it into MS calculator?

, Nov 16, 2006

3. ### Peter NilssonGuest

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

Peter Nilsson, Nov 16, 2006
4. ### Keith ThompsonGuest

"" <> writes:
> 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) <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.

Keith Thompson, Nov 16, 2006
5. ### CBFalconerGuest

Peter Nilsson wrote:
> 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>

CBFalconer, Nov 17, 2006

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>

7. ### santoshGuest

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.

santosh, Nov 17, 2006
8. ### CBFalconerGuest

santosh wrote:
> 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>

CBFalconer, Nov 17, 2006