Mail Archives: djgpp/1999/08/06/07:27:07
> From: jsc AT lds DOT co DOT uk
> Subject: Forming a float from 4 seperate bytes
> Date: Tue, 27 Jul 1999 18:37:16 GMT
> Organization: Deja.com - Share what you know. Learn what you don't.
> To: djgpp AT delorie DOT com
> Reply-to: djgpp AT delorie DOT com
> Hello all, sorry for the post that isn't specifically related to DJGPP, but
> it's the only C news group I'm on.
>
> I'm writing an (old) 3DStudio4 file parser and found a problem in the way
> that the vertex coordinates are listed. each floating point value is
> represented by 4 bytes which I can access but can't combine to form the
> original float.
>
> Well, I sort of can. If I do it directly from the file and use
>
> fread (&Xvalue,4,1,fp);
>
> where fp is my file pointer, 4 = the sizeof(float) , 1 = I want to do this
> once and Xvalue = the final float value I want to end up with, it works. I
>
I guess it's because that fread() reads the 4 bytes as one package,
and the ordering of each bytes are not changed.
> don't know why - I just took it from a doc on the 3ds format. However I want
> to unload the file to a buffer and then start parsing. I've done the reading
> into the buffer, but now I don't know how to reform my float from the data, I
> tried taking the 4 values and &-ing them together, but that doesn't work as
> the final value I'm after is a float and I get an operand error. Any ideas on
> how to take 4 integers and form the float? I'm surre it's easy but I can't
> get it to work. Thanks Chris jsc AT lds DOT co DOT uk
>
Taking the 4 values then ANDing them together won't work because of
the storing format of int and float differs greatly. The IEEE
standard for storing a 32 bit float is :
bit 0 - 22 significand
bit 23 - 30 exponent
bit 31 sign
As you can see, ANDing or any other bitwise operation with the 4
integers won't produce a float as integers use all of it bits to
represent the integer value, unlike float.
I have a similar problem once. The problem is how to read & combine a
32 bit integer that is consists of 4 bytes. My approach to the
problem is to use a bit assembly.
Let's assume that BytePtr is the pointer to the series of 4 bytes
that makes the integer in the memory buffer.
;------------------------------------------------------------
; prototype : int combineInt(char *BytePtr)
;------------------------------------------------------------
combineInt:
push ebp
mov ebp,esp
sub eax,eax
mov esi,[ebp+BYTEPTR]
mov ah,[esi+3]
mov al,[esi+2]
shl eax,16
mov ah,[esi+1]
mov al,[esi]
mov esp,ebp
pop ebp
ret
All the above function do is taking each byte from the memory buffer
and store it at the proper place in a 32 bit register.
To modify it to combine a float instead, then just modify the
prototype so that it returns a float instead of an int.
prototype : float CombineInt(char *BytePtr);
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
>
Hope that helps.
Batchex
thedark1 AT Phreaker DOT net
- Raw text -