Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Some help with char[4] to float please

Reply
Thread Tools

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

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-01-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
Re: Some help with char[4] to float please WenwuChen C++ 4 08-02-2003 02:11 AM
Re: Some help with char[4] to float please lurkerboy C++ 0 08-01-2003 03:48 AM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57