-1

I have a fairly big (Around 50bytes) structure in a Embedded device to be transported to a PHP script which is to be stored in a DB. Most of the elements in the structure are bits and some are integers and chars. What I plan is use a union in C (Embedded Device) to take the data as a binary array and do a base64 encoding and upload through a URL string as a variable. Now In php I have a big array which needs to be sepertaed as flags and integers to be store in DB. This is my task. which will be the suitable method to do the work. Thanks

UB_Roy
  • 651
  • 1
  • 6
  • 12
  • Why not generate a json in C and parse in PHP ? – Ôrel Aug 20 '15 at 06:30
  • json and xml contain the tag info also? I need the payload to be small. – UB_Roy Aug 20 '15 at 07:01
  • Why you need a small payload ? You can write the payload progressively without using too many memory. OR you can simply write the binary on the payload en then parse it on the PHP side. – Ôrel Aug 20 '15 at 07:21
  • currently the transmit socket buffer size is small. May be able to increase it. Will try. Or else I have to send data as two or three chunks. or i think I will have to do a manual parsing at the php side. but in that case as the payload variables may be modified in future we have to count the bits and make the changes. That is why I like to know weather any methods in php to make this task easier – UB_Roy Aug 20 '15 at 08:21

2 Answers2

0

If you send as binary, then you will have to decide on and enforce byte order at each end regardless of platform byte order. Enforce network byte order by using the POSIX byte order conversion functions.

Also you cannot rely on compiler generated structure bit-fields to pack and unpack identically on both platforms so for the bit fields you must use shifting-and-masking to pack and unpack and to be careful of bitfields that span byte or word boundaries.

A more easily portable solution perhaps is to transfer using a structured text format such as XML or JSON, or a proprietary format of your own design if these are too verbose.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Embedded devices with limited sockets are probably non-POSIX – Marco van de Voort Sep 02 '15 at 19:31
  • @MarcovandeVoort : Perhaps, but the byte order function typically implemented in any case and are trivial to implement otherwise. – Clifford Sep 02 '15 at 21:28
  • I don't thing that he need shift and unmask, because if he make an embedded application for one platform whit certain compiler, then bit-fields order and structure packing will never change. – vlk Sep 12 '15 at 05:13
  • @vik : The point is not about the bit-packing on the same system with the same compiler, but rather the embedded system with a connected system running a PHP script to interpret the data. Moreover, it is likely that the code will live longer then the system it was originally implemented on and may be reused elsewhere. – Clifford Sep 12 '15 at 08:04
0

Structures created in C you can decode in php using unpack() function.

vlk
  • 2,581
  • 3
  • 31
  • 35