454,605 Members | 1,415 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

# convert 64-integer to hex octet string

 P: n/a 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 Replies

 P: n/a correction: but "ffffffff" is NOT the max if I directly input it into MS calculator? Nov 16 '06 #2

 P: n/a 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

 P: n/a "we*****@yahoo.com" . -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* We must do something. This is something. Therefore, we must do this. Nov 16 '06 #4

 P: n/a 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 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. Nov 17 '06 #5

 P: n/a 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 #include 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 Nov 17 '06 #6

 P: n/a 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

 P: n/a 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. Nov 17 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion.