Convert Float (or Integer) to Byte array

AV
 
Avatar
 
Betreff:

Convert Float (or Integer) to Byte array

 · 
Gepostet: 03.12.2012 - 15:55 Uhr  ·  #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
Geschlecht: keine Angabe
Herkunft: Sauerland NRW
Beiträge: 372
Dabei seit: 03 / 2008
Betreff:

Re: Convert Float (or Integer) to Byte array

 · 
Gepostet: 03.12.2012 - 16:03 Uhr  ·  #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
 
Betreff:

Re: Convert Float (or Integer) to Byte array

 · 
Gepostet: 03.12.2012 - 16:31 Uhr  ·  #3
Good Boy - thank you (-:

Andreas
Gunter
Administrator
Avatar
Geschlecht:
Herkunft: Frankfurt Main / Germany
Beiträge: 1697
Dabei seit: 02 / 2003
Betreff:

Re: Convert Float (or Integer) to Byte array

 · 
Gepostet: 03.12.2012 - 16:32 Uhr  ·  #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
Geschlecht: keine Angabe
Herkunft: Sauerland NRW
Beiträge: 372
Dabei seit: 03 / 2008
Betreff:

Re: Convert Float (or Integer) to Byte array

 · 
Gepostet: 03.12.2012 - 16:40 Uhr  ·  #5
Was dagegen spricht? - Die runden Klammern um den @-Ausdruck! :P
Sonst nix! Ist bei einfachen Ausdrücken der bessere Weg..
Gunter
Administrator
Avatar
Geschlecht:
Herkunft: Frankfurt Main / Germany
Beiträge: 1697
Dabei seit: 02 / 2003
Betreff:

Re: Convert Float (or Integer) to Byte array

 · 
Gepostet: 03.12.2012 - 16:41 Uhr  ·  #6
einmal nicht nachgeschaut :sick:
Avra
Schreiberling
Avatar
Geschlecht:
Herkunft: Belgrade, Serbia
Alter: 54
Homepage: rs.linkedin.com/in…
Beiträge: 653
Dabei seit: 07 / 2002
Betreff:

Re: Convert Float (or Integer) to Byte array

 · 
Gepostet: 03.12.2012 - 17:47 Uhr  ·  #7
Zitat geschrieben von 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
 
Betreff:

Re: Convert Float (or Integer) to Byte array

 · 
Gepostet: 04.12.2012 - 12:09 Uhr  ·  #8
It's interesting so see the different ways ...
Gewählte Zitate für Mehrfachzitierung:   0

Registrierte in diesem Topic

Aktuell kein registrierter in diesem Bereich

Die Statistik zeigt, wer in den letzten 5 Minuten online war. Erneuerung alle 90 Sekunden.
MySQL Queries: 15 · Cache Hits: 14   124   138 · Page-Gen-Time: 0.04537s · Speichernutzung: 2 MB · GZIP: ein · Viewport: SMXL-HiDPI