comp.lang.ada
 help / color / mirror / Atom feed
* converting floats, ints byte arrays
@ 2001-07-12  5:46 Vladimir Bednikov
  2001-07-12  6:45 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Vladimir Bednikov @ 2001-07-12  5:46 UTC (permalink / raw)


Hi all,

I have a bunch of values I want to send via tcp, instead of converting
each to a string and sending it out, I want to store all the values into
a byte array, ie:
type byte is mod 256;
type byte_array is array(integer range <>) of byte;

The values are for example,

sampleRate : C.C_Float;
scalingFactor : C.C_Float;
numSamples : C.Unsigned;
numSensors : C.Unsigned;
positionChanged : C.Unsigned_Char;
groupBySensors : C.Unsigned_Char;

What I want is an array where the first C.C_Float'size bytes is the sample
rate,
the next C.C_Float'size bytes is the scaling factor, the next
C.Unsigned'size bytes
is the number of samples etc and finally send the byte array out via tcp.

Does anybody out there know of a way to do this.

All help is appreciated,
Thanks in advance





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: converting floats, ints byte arrays
  2001-07-12  5:46 converting floats, ints byte arrays Vladimir Bednikov
@ 2001-07-12  6:45 ` tmoran
  2001-07-12 16:35 ` Jeffrey Carter
  2001-07-13 11:34 ` David C. Hoos
  2 siblings, 0 replies; 4+ messages in thread
From: tmoran @ 2001-07-12  6:45 UTC (permalink / raw)


>a byte array, ie:
>type byte is mod 256;
>type byte_array is array(integer range <>) of byte;
>sampleRate : C.C_Float;
>scalingFactor : C.C_Float;
>What I want is an array where the first C.C_Float'size bytes is the sample
>rate,
  This is a perfect job for Streams.  It will produce an
Ada.Streams.Stream_Element_Array rather than a "byte_array", but
it's essentially the same thing.  In some cases a size might not be
the same as 'size, because the compiler can use the base_type'size.
e.g.
type little_integers is new integer range 0 .. 3;
for little_integers'size use 2;
would have a 'size of 2, but Streams would likely use Integer'size
instead.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: converting floats, ints byte arrays
  2001-07-12  5:46 converting floats, ints byte arrays Vladimir Bednikov
  2001-07-12  6:45 ` tmoran
@ 2001-07-12 16:35 ` Jeffrey Carter
  2001-07-13 11:34 ` David C. Hoos
  2 siblings, 0 replies; 4+ messages in thread
From: Jeffrey Carter @ 2001-07-12 16:35 UTC (permalink / raw)


Vladimir Bednikov wrote:
> 
> What I want is an array where the first C.C_Float'size bytes is the sample

Remember that 'Size gives the size in bits, not bytes. To get the size
in terms of your Byte type, you need to calculate

(C.C_Float'Size + Byte'Size - 1) / Byte'Size

-- 
Jeffrey Carter



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: converting floats, ints byte arrays
  2001-07-12  5:46 converting floats, ints byte arrays Vladimir Bednikov
  2001-07-12  6:45 ` tmoran
  2001-07-12 16:35 ` Jeffrey Carter
@ 2001-07-13 11:34 ` David C. Hoos
  2 siblings, 0 replies; 4+ messages in thread
From: David C. Hoos @ 2001-07-13 11:34 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Vladimir.Bednikov

Hi Vladimir,

Based on your earlier queries about the adasockets package,
I am answering your question in the context of adacokets.

If, after creating the socket and establishing the connection,
you call Sockets.Stream_IO.Initialize, that call will associate
a stream with the connected socket.

Then if your data is placed in an Ada record declared
like this:

type my_type is record
   sampleRate : C.C_Float;
   scalingFactor : C.C_Float;
   numSamples : C.Unsigned;
   numSensors : C.Unsigned;
   positionChanged : C.Unsigned_Char;
   groupBySensors : C.Unsigned_Char;
end record;

My_Object : My_Type;

Then, when my_object is populates with the values you can
simply write

My_Type'Write (Stream, My_Object);

where Stream is the stream object associated with the socket
by calling Initialize.

This will create the TCP packet just as you specified.

This is demonstrated in the adasockets examples directory
in the program stream_sender.adb.

----- Original Message -----
From: "Vladimir Bednikov" <Vladimir.Bednikov@ebor.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Thursday, July 12, 2001 12:46 AM
Subject: converting floats, ints byte arrays


> Hi all,
>
> I have a bunch of values I want to send via tcp, instead of converting
> each to a string and sending it out, I want to store all the values into
> a byte array, ie:
> type byte is mod 256;
> type byte_array is array(integer range <>) of byte;
>
> The values are for example,
>
> sampleRate : C.C_Float;
> scalingFactor : C.C_Float;
> numSamples : C.Unsigned;
> numSensors : C.Unsigned;
> positionChanged : C.Unsigned_Char;
> groupBySensors : C.Unsigned_Char;
>
> What I want is an array where the first C.C_Float'size bytes is the sample
> rate,
> the next C.C_Float'size bytes is the scaling factor, the next
> C.Unsigned'size bytes
> is the number of samples etc and finally send the byte array out via tcp.
>
> Does anybody out there know of a way to do this.
>
> All help is appreciated,
> Thanks in advance
>
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-07-13 11:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-12  5:46 converting floats, ints byte arrays Vladimir Bednikov
2001-07-12  6:45 ` tmoran
2001-07-12 16:35 ` Jeffrey Carter
2001-07-13 11:34 ` David C. Hoos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox