Convert Float (or Integer) to Byte array

AV
 
Avatar
 
Subject:

Convert Float (or Integer) to Byte array

 · 
Posted: 03.12.2012 - 14:55  ·  #1
I want to convert a Float (and Integer) value to a Byte array.

Or in other words:
The Float numbers are internally represented by 4 Bytes and Integer numbers by 2 Bytes.
These Bytes I want to copy into a custom data frame (encoded as Byte array) and send it by UDP (using TINA).

After some searches I found the Pascal command move, which could be used for the conversion task.
The AVRco compiler manual just has a side note that the move command is supported.
Unfortunately the compiler doesn't like my code and I didn't find any AVRco related information in the web.

Code

// definition
testFloat :Float;
floBytes  :Array[0..3] of Byte;

// conversion test
testFloat := 3;
move(testFloat, floBytes, 4);  // ERROR: var or symbol expected


I would be happy about some hints.
mc-electronic
Benutzer
Avatar
Gender: n/a
Location: Sauerland NRW
Posts: 372
Registered: 03 / 2008
Subject:

Re: Convert Float (or Integer) to Byte array

 · 
Posted: 03.12.2012 - 15:03  ·  #2
Hi,

I use the AVRco Command CopyBlock:

Var
testFloat :Float;
floBytes :Array[0..3] of Byte;
Begin
TestFloat := 3;
CopyBlock(@TestFloat,@FloByte[0], SizeOf(TestFloat));
...

Regards, Michael
AV
 
Avatar
 
Subject:

Re: Convert Float (or Integer) to Byte array

 · 
Posted: 03.12.2012 - 15:31  ·  #3
Good Boy - thank you (-:

Andreas
Gunter
Administrator
Avatar
Gender:
Location: Frankfurt Main / Germany
Posts: 1697
Registered: 02 / 2003
Subject:

Re: Convert Float (or Integer) to Byte array

 · 
Posted: 03.12.2012 - 15:32  ·  #4
Hi,

was spricht gegen
Var
testFloat :Float;
floBytes(@testFloat) :Array[0..3] of Byte; //overlay


Braucht keinen Speicher und keine Laufzeit.

Gunter
mc-electronic
Benutzer
Avatar
Gender: n/a
Location: Sauerland NRW
Posts: 372
Registered: 03 / 2008
Subject:

Re: Convert Float (or Integer) to Byte array

 · 
Posted: 03.12.2012 - 15:40  ·  #5
Was dagegen spricht? - Die runden Klammern um den @-Ausdruck! :P
Sonst nix! Ist bei einfachen Ausdrücken der bessere Weg..
Gunter
Administrator
Avatar
Gender:
Location: Frankfurt Main / Germany
Posts: 1697
Registered: 02 / 2003
Subject:

Re: Convert Float (or Integer) to Byte array

 · 
Posted: 03.12.2012 - 15:41  ·  #6
einmal nicht nachgeschaut :sick:
Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: Convert Float (or Integer) to Byte array

 · 
Posted: 03.12.2012 - 16:47  ·  #7
Quote by AV
I want to convert a Float (and Integer) value to a Byte array.


You can exploit overlays as seen here:
Code
  TFix64Overlay = record // this is handy for fast extraction of integer and fractional parts
                    fix        : fix64;
                    x  [@fix]  : fix64;
                    i64[@fix]  : int64;
                    w64[@fix]  : word64;
                    i  [@fix+4]: longint;
                    i32[@fix+4]: longint;
                    f  [@fix]  : longword;
                    f32[@fix]  : longword;
                    b  [@fix]  : array[0..7] of byte;
                    w  [@fix]  : array[0..3] of word;
                  end;


In your situation it renders to something like this:
Code
type
  TFloatOverlay = record
                    single        : float;
                    b  [@single]  : array[0..3] of byte;
                  end;
  TIntOverlay = record
                    int           : integer;
                    b  [@single]  : array[0..1] of byte;
                  end;
var
   OvrFlt: TFloatOverlay;
   OvrInt: TIntOverlay;
   FirstFloatByte, SecondFloatByte, ThirdFloatByte, FourthFloatByte: TFloatOverlay;
   FirstIntByte, SecondIntByte: TIntOverlay;
...

begin
  OvrFlt.Single   := 3.14;
  FirstFloatByte  := OvrFlt.b[0];
  SecondFloatByte := OvrFlt.b[1];
  ThirdFloatByte  := OvrFlt.b[2];
  FourthFloatByte := OvrFlt.b[3];

  OvrInt.Int    := -9876;
  FirstIntByte  := OvrInt.b[0];
  SecondIntByte := OvrInt.b[1];
end;
AV
 
Avatar
 
Subject:

Re: Convert Float (or Integer) to Byte array

 · 
Posted: 04.12.2012 - 11:09  ·  #8
It's interesting so see the different ways ...
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   124   138 · Page-Gen-Time: 0.033332s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI