Board index » delphi » float to four bytes array?

float to four bytes array?

Hi all,

How I can convert single to the four bytes array?
Say 1.5 to [63,192,0,0] or 10,3 to [65,36,204,205] for example.
Need short and quick solution in Pascal.

Thanks,
Andrew

 

Re:float to four bytes array?


Quote
and...@SPAMNOyahoo.com (Andrew) wrote:
>How I can convert single to the four bytes array?
>Say 1.5 to [63,192,0,0] or 10,3 to [65,36,204,205] for example.
>Need short and quick solution in Pascal.

One way (except I don't know about the order of the bytes):

x : single;
y : array[ 1 .. 4] of boolean;

move( x, y, SizeOf( single));

If you need it faster, I think an absolute address would be
faster.

Re:float to four bytes array?


Quote
and...@SPAMNOyahoo.com (Andrew) wrote:
>How I can convert single to the four bytes array?

This is probably faster:

type a = array[ 1 .. 4] of byte;

var x : single; y : a;

y := a( x);

but you may have to reverse the bytes.

Re:float to four bytes array?


On Sat, 16 Jun 2001 04:57:42 +0000 (UTC), and...@SPAMNOyahoo.com

Quote
(Andrew) wrote:
>Hi all,

>How I can convert single to the four bytes array?
>Say 1.5 to [63,192,0,0] or 10,3 to [65,36,204,205] for example.
>Need short and quick solution in Pascal.

I'm unclear what the relationship is between the floating point values
and the values of the bytes, what is it you're trying to accomplish?

Stephen Posey
slpo...@concentric.net

Re:float to four bytes array?


slpo...@concentric.net (Stephen Posey) wrote in
<3b2c0650.15939...@news.concentric.net>:

Quote
>I'm unclear what the relationship is between the floating point values
>and the values of the bytes, what is it you're trying to accomplish?

>Stephen Posey
>slpo...@concentric.net

I have a float value and need to write it to the file as four byte.

Andrew

Re:float to four bytes array?


Thanks Jan,

exactly what I looked for.

Andrew.

Re:float to four bytes array?


On Sat, 16 Jun 2001 10:24:26 +0000 (UTC), and...@SPAMNOyahoo.com

Quote
(Andrew) wrote:
>slpo...@concentric.net (Stephen Posey) wrote in
><3b2c0650.15939...@news.concentric.net>:
>>I'm unclear what the relationship is between the floating point values
>>and the values of the bytes, what is it you're trying to accomplish?

>>Stephen Posey
>>slpo...@concentric.net

>I have a float value and need to write it to the file as four byte.

If you're not writing to a Text file then just writing the value will
achieve precisely that, something like:

var
  F : file of single;
  S: single;
begin
  S := 3.14159;
  Assign(F, 'SINGLE.DAT');
  ReSet(F);
  Write(F, S);
  Close(F);

will result in a file containing the four byte values represented by
S.

If you're working with an untyped file, you can use BlockWrite to
achieve the same effect:

  BlockWrite(F, S, SizeOf(single));

Is that what you're after?  

Stephen Posey
slpo...@concentric.net

Re:float to four bytes array?


Quote
and...@SPAMNOyahoo.com (Andrew) wrote:
>exactly what I looked for.

Make sure the bytes are in the right order.  I forget the big
endian and little endian stuff.

Re:float to four bytes array?


Thanks Stephen,

This is also a good way.
But how I can control byte order if I will write single directly to the
file?

Andrew.

Re:float to four bytes array?


On Sun, 17 Jun 2001 03:27:32 +0000 (UTC), and...@SPAMNOyahoo.com

Quote
(Andrew) wrote:
>Thanks Stephen,

>This is also a good way.
>But how I can control byte order if I will write single directly to the
>file?

Where does byte order come into the equation?  What's going to be
accessing these singles you're writing to the file?

Stephen Posey
slpo...@concentric.net

Re:float to four bytes array?


Quote
In article <3b2d4eba.14315...@news.concentric.net>, Stephen Posey wrote:
> On Sun, 17 Jun 2001 03:27:32 +0000 (UTC), and...@SPAMNOyahoo.com
> (Andrew) wrote:

>>Thanks Stephen,

>>This is also a good way.
>>But how I can control byte order if I will write single directly to the
>>file?

> Where does byte order come into the equation?  What's going to be
> accessing these singles you're writing to the file?

IIRC Byte order doesn't matter for IEEE floats

Re:float to four bytes array?


In article <slrn9iouu9.2rgs.mar...@toad.stack.nl>,
Marco van de Voort <mar...@toad.stack.nl> wrote:

Quote
>In article <3b2d4eba.14315...@news.concentric.net>, Stephen Posey wrote:
>> On Sun, 17 Jun 2001 03:27:32 +0000 (UTC), and...@SPAMNOyahoo.com
>> (Andrew) wrote:

>>>Thanks Stephen,

>>>This is also a good way.
>>>But how I can control byte order if I will write single directly to the
>>>file?

>> Where does byte order come into the equation?  What's going to be
>> accessing these singles you're writing to the file?

>IIRC Byte order doesn't matter for IEEE floats

Pardon? Did you mean that the byte order is defined in IEEE floats?

Osmo

Re:float to four bytes array?


Quote
In article <9gi0nr$r7...@oravannahka.helsinki.fi>, Osmo Ronkanen wrote:
> In article <slrn9iouu9.2rgs.mar...@toad.stack.nl>,
> Marco van de Voort <mar...@toad.stack.nl> wrote:
>>In article <3b2d4eba.14315...@news.concentric.net>, Stephen Posey wrote:
>>> On Sun, 17 Jun 2001 03:27:32 +0000 (UTC), and...@SPAMNOyahoo.com
>>> (Andrew) wrote:

>>>>Thanks Stephen,

>>>>This is also a good way.
>>>>But how I can control byte order if I will write single directly to the
>>>>file?

>>> Where does byte order come into the equation?  What's going to be
>>> accessing these singles you're writing to the file?

>>IIRC Byte order doesn't matter for IEEE floats

> Pardon? Did you mean that the byte order is defined in IEEE floats?

Afaik yes, but I'm not entirely sure.

Re:float to four bytes array?


The way I do it is by creating a "free union" (I think that's the term
-- in Fortran, it was called an Equivalence ...).

TYPE
  singleorbytestype = RECORD
     CASE (sgl, byt) OF
      sgl : (singlepart : single);
      byt: (bytepart : array [1 .. 4] OF byte)
    END;

VAR
   sorb : singleorbytestype;

BEGIN
   sorb.singlepart := 1.5;
   FOR index := 1 TO 4 DO writeln (sorb.bytepart[index]);
END;

Bob Schor
Pascal Enthusiast

Quote
Andrew wrote:
> Hi all,

> How I can convert single to the four bytes array?
> Say 1.5 to [63,192,0,0] or 10,3 to [65,36,204,205] for example.
> Need short and quick solution in Pascal.

> Thanks,
> Andrew

Re:float to four bytes array?


Quote
>IIRC Byte order doesn't matter for IEEE floats

No. Open eny hex editor type say in hex 41,20,0,0 and read the float value
of this (10.0) then type in back order 0,0,20,41 and read again
(1.1570521e-041).
Byte order is important, at least for my task.
Go to page: [1] [2]

Other Threads