convert 64-integer to hex octet string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wenmang@yahoo.com

    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 = 184467440737095 51615;
    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 184467440737095 51615? it should be
    0xfffffffffffff fff, isn't it?
    The next thing I like to do is to represent the max
    number(18446744 073709551615) as octet string, how can I do it?
    I tried following but it seems not right:
    unsigned long long maxNum=18446744 073709551615;
    unsigned char hexNumStr[sizeof(unsigned long long)];
    memcpy(&hexNumS tr, &maxNum, sizeof(unsigned long long));
    how am I going to print out the content of hexNum as a string?
    thx

  • wenmang@yahoo.com

    #2
    Re: convert 64-integer to hex octet string

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

    Comment

    • Peter Nilsson

      #3
      Re: convert 64-integer to hex octet string

      wenmang@yahoo.c om 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 = 184467440737095 51615;
      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

      Comment

      • Keith Thompson

        #4
        Re: convert 64-integer to hex octet string

        "wenmang@yahoo. com" <wenmang@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 = 184467440737095 51615;
        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_Keit h) kst-u@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.

        Comment

        • CBFalconer

          #5
          Re: convert 64-integer to hex octet string

          Peter Nilsson wrote:
          wenmang@yahoo.c om wrote:
          >>
          >I have a confusion for representation of hex number:
          >For an unsigned long long number, its max value is:
          >unsigned long long maxNum = 184467440737095 51615;
          >
          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>


          Comment

          • Tor Rustad

            #6
            Re: convert 64-integer to hex octet string

            wenmang@yahoo.c om 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 = 184467440737095 51615;
            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 184467440737095 51615? it should be
            0xfffffffffffff fff, 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(18446744 073709551615) as octet string, how can I do it?
            Look at snprintf(...) and above.

            --
            Tor <torust AT online DOT no>

            Comment

            • santosh

              #7
              Re: convert 64-integer to hex octet string

              wenmang@yahoo.c om 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 = 184467440737095 51615;
              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 184467440737095 51615? it should be
              0xfffffffffffff fff, isn't it?
              The next thing I like to do is to represent the max
              number(18446744 073709551615) 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=18446744 073709551615;
              unsigned char hexNumStr[sizeof(unsigned long long)];
              memcpy(&hexNumS tr, &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.

              Comment

              • CBFalconer

                #8
                Re: convert 64-integer to hex octet string

                santosh wrote:
                wenmang@yahoo.c om wrote:
                >>
                >I have a confusion for representation of hex number:
                >For an unsigned long long number, its max value is:
                >unsigned long long maxNum = 184467440737095 51615;
                >
                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>


                Comment

                Working...