<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Here is a very simple conversion from a dynamic
byte array to a Single:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>function BytesToSingle(Data:
TByteDynArray;<BR> Position: Integer):
Single;<BR>{-------------------------------------------------------------------------<BR> DESCRIPTION:
Accepts a byte array and returns a Single.<BR> PARAMETERS: The
byte array and an integer specifying the position in the array to
start.<BR> RETURNS:
Single<BR>-------------------------------------------------------------------------}<BR>const<BR>
DATA_SIZE: Longint = 4;<BR>begin<BR> if Position <= High(Data) -
(DATA_SIZE - 1) then<BR> Move(Data[Position], result,
DATA_SIZE);<BR>end;<BR></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>And a routine to convert from a Single to
Bytes:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>function SingleToBytes(Data: Single):
TByteDynArray;<BR>{---------------------------------------------------------------------------<BR>
DESCRIPTION: Converts the Single to a 4byte
array.<BR> PARAMETERS:
Data The Single to
convert<BR> RETURNS:
TByteDynArray The byte values separated into a byte
array<BR>---------------------------------------------------------------------------}<BR>const<BR>
DATA_SIZE : LongInt = 4;<BR>begin<BR> SetLength(result,
DATA_SIZE);<BR> Move(Data, result[0], DATA_SIZE);<BR>end;<BR></FONT></DIV>
<DIV><FONT face=Arial size=2>The above routine could also easily be adapted to
work with an existing byte array and take a pPosition parameter like the
first routine.</DIV></FONT>
<DIV><FONT face=Arial size=2> </DIV></FONT>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=colmin@ihug.co.nz href="mailto:colmin@ihug.co.nz">Colin/Mina</A>
</DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=Delphi@delphi.org.nz
href="mailto:Delphi@delphi.org.nz">Delphi@delphi.org.nz</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Tuesday, August 31, 2004 9:29
PM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> [DUG] RE: [DUG-Offtopic] Byte to
Real conversion</DIV>
<DIV><BR></DIV>
<DIV><FONT face=Arial size=2>Hi,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Any ideas please?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I am developing an embedded processor controller
that needs to continuously communicate data over a link to
another. The baud rate is limited to 9600 Bd. The numeric range of each data
packet will need to be at least +/ 12 digits. I am concerned that the time
taken to send this data may too high if sent as strings of characters and I
would like to limit this.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>My data values could be easily
represented within the four bytes of a 'Single' which is the smallest Real
type and would give me a range of </FONT><FONT face=Arial size=2>1.5 x 10 ^-45
.. 3.4 x 10 ^38. This being the case I could send the data as 4 x
char(bytes) + a checksum and terminator.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>My question is - What is the simplest way of
doing the conversion in each direction. <FONT face=Arial size=2>I have to deal
with two different microprocessors, but t</FONT></FONT><FONT face=Arial
size=2>he maths libraries I have in the compilers are not too bad. I've
had a play but seem to be getting into problems with type casting. A
Pascal demo would be a great help.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Regards</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> Colin</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<P>
<HR>
<P></P>_______________________________________________<BR>Delphi mailing
list<BR>Delphi@ns3.123.co.nz<BR>http://ns3.123.co.nz/mailman/listinfo/delphi<BR></BLOCKQUOTE></BODY></HTML>