read 32 bit value into 64 bit variable?

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

    #1

    read 32 bit value into 64 bit variable?

    Hello all,

    need to read a 321bit value from a file into a 64 bit variable (either
    float or int). in addition, the endianness of the run-time platform
    could differ from the endianness of the value - let's assume it's known
    at runtime whether the endianness is the same or different. any ideas?

    TIA,
    andrey
    --
    comp.lang.c.mod erated - moderation address: clcm@plethora.n et -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.
  • Grumble

    #2
    Re: read 32 bit value into 64 bit variable?

    ae5880@yahoo.co m wrote:
    [color=blue]
    > need to read a 32 bit value from a file into a 64 bit variable (either
    > float or int). in addition, the endianness of the run-time platform
    > could differ from the endianness of the value - let's assume it's known
    > at runtime whether the endianness is the same or different. any ideas?[/color]

    Assume your value is 1,500,000,000.

    int i = 1500000000; /* 32-bits wide ints */
    fprintf(fp, "%i\n", i);

    then, on the other system,

    int i; /* 64-bits wide ints */
    fscanf(fp, "%i\n", &i);

    or

    float f;
    fscanf(fp, "%f\n", &f);

    Comment

    • Netocrat

      #3
      Re: read 32 bit value into 64 bit variable?

      On Sun, 26 Jun 2005 09:57:52 +0000, ae5880 wrote:
      [color=blue]
      > Hello all,
      >
      > need to read a 321bit value from a file into a 64 bit variable (either
      > float or int). in addition, the endianness of the run-time platform could
      > differ from the endianness of the value - let's assume it's known at
      > runtime whether the endianness is the same or different. any ideas?[/color]

      If the file representation of the 32-bit value doesn't change between
      run-time platforms then you don't need to worry how the machine represents
      it in memory. Just don't use any techniques that make any assumptions
      about byte-position - eg. use bit shifting if you have to interpret the
      32-bit value in the file as 4 separate bytes.
      --
      comp.lang.c.mod erated - moderation address: clcm@plethora.n et -- you must
      have an appropriate newsgroups line in your header for your mail to be seen,
      or the newsgroup name in square brackets in the subject line. Sorry.

      Comment

      • Hans-Bernhard Broeker

        #4
        Re: read 32 bit value into 64 bit variable?

        In comp.lang.c.mod erated ae5880@yahoo.co m wrote:[color=blue]
        > need to read a 321bit value from a file into a 64 bit variable (either
        > float or int). in addition, the endianness of the run-time platform
        > could differ from the endianness of the value - let's assume it's known
        > at runtime whether the endianness is the same or different. any ideas?[/color]

        Read it byte-wise, cast each byte to the result type, shift it to the
        right position, and logical-or it all together.

        [And please don't cross-post between an open and a moderated group; or
        if you think you have to, at least reduce the F'up2 to a single group.
        Thank you]

        --
        Hans-Bernhard Broeker (broeker@physik .rwth-aachen.de)
        Even if all the snow were burnt, ashes would remain.
        --
        comp.lang.c.mod erated - moderation address: clcm@plethora.n et -- you must
        have an appropriate newsgroups line in your header for your mail to be seen,
        or the newsgroup name in square brackets in the subject line. Sorry.

        Comment

        • Old Wolf

          #5
          Re: read 32 bit value into 64 bit variable?

          ae5880@yahoo.co m wrote:[color=blue]
          >
          > need to read a 321bit value from a file into a 64 bit variable (either
          > float or int). in addition, the endianness of the run-time platform
          > could differ from the endianness of the value - let's assume it's known
          > at runtime whether the endianness is the same or different. any ideas?[/color]

          You have to know the endianness of the source file.
          Assuming it's big-endian and your PC has 8-bit chars:

          long long read_64(FILE *fp)
          {
          unsigned long long var;
          var = getc(fp); var <<= 8;
          var |= getc(fp); var <<= 8;
          var |= getc(fp); var <<= 8;
          var |= getc(fp);

          if (feof(fp))
          return ERROR_READING_F ILE;

          return var;
          }

          Of course there are 1001 (at least) other ways to write
          this function.
          --
          comp.lang.c.mod erated - moderation address: clcm@plethora.n et -- you must
          have an appropriate newsgroups line in your header for your mail to be seen,
          or the newsgroup name in square brackets in the subject line. Sorry.

          Comment

          Working...