Re: Some help with char[4] to float please

Discussion in 'C++' started by Alf P. Steinbach, Aug 1, 2003.

  1. On Thu, 31 Jul 2003 21:41:28 -0500, lurkerboy <> wrote:

    >I am reading in 4 bytes from a file into a char array, i.e., char
    >holdme[4] . I need to cast these 4 bytes so I can read them as a
    >float type.


    Generally, read data back from the file the same way it was stored.

    Presumably these four bytes weren't stored as four individual bytes,
    but as a 'float' value.

    Why don't you just read back that value _as_ a 'float' value?



    >For example in VC++, debug, I can see that holdme = {0x10, 0xA2, 0x72,
    >0x94}. I want to read those four bytes as a float variable. As I
    >interpret the help manual, a float type is 4 bytes, but I'm getting
    >cast error messages when I do something like this:
    >
    >int main()
    >{
    >char holdme[4];
    >float myfloat;
    >...
    >*myfloat = four_chars2float(char holdme)


    'myfloat' is not a pointer.


    >...
    >
    >}
    >
    >
    >float four_chars2long(char value[] )


    This is equivalent to


    float four_chars2long( char* value )



    >{
    > float *midwaypoint;
    > midwaypoint = (float) &value;


    The address of 'value' is the address of a pointer, which for your
    purposes is meaningless.

    The cast to 'float' is meaningless.

    Finally, assigning a 'float' to a 'float*' pointer is meaningless.



    > return *midwaypoint;
    >}
    >
    >If there is better way, please show me as I am a rank beginner with
    >VC++.


    See above.

    Also keep in mind that float is not necessarily 4 bytes with other
    compilers.

    If you control the creation of the file, I suggest you stick to text
    files.
     
    Alf P. Steinbach, Aug 1, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lurkerboy
    Replies:
    0
    Views:
    378
    lurkerboy
    Aug 1, 2003
  2. WenwuChen
    Replies:
    4
    Views:
    495
    Kevin Goodsell
    Aug 2, 2003
  3. bd
    Replies:
    0
    Views:
    651
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,099
    Ian Collins
    May 9, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,610
    James Kanze
    Oct 8, 2009
Loading...

Share This Page