0

I get as an argument data which is of type char[].

Given the information data is organized as a Packet object. How do I convert data to be a Packet? Packet includes primitive fields, and array of object consisting of primitive fields too. Is there a way doing it in Java? I know in C, C++ it is easy...

    class Square{
        float x;
        float y;
        int size;
    }    

   class Packet{
        int first;
        int last;
        Square s[]; 
    }
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • 3
    It's really unclear what you'd expect here. You've got *textual* data, but you want to get floating point values out of it... what would that text look like for a `Square` with x=2.5, y=10.25, size=10 for example? If you've just performed some sort of plain "in memory dump" serialization, that's not great as a serialization format... and you'd almost certainly want `byte[]` instead of `char[]`. – Jon Skeet Sep 22 '20 at 17:01
  • Java has no `reinterpret_cast`. You can not _just_ convert arbitrary data to a different type. Improve your design instead. Also, please elaborate. – Zabuzard Sep 22 '20 at 17:05
  • Possible duplicate: [Is there cast in Java similar to in C++](https://stackoverflow.com/questions/4805058/is-there-cast-in-java-similar-to-reinterpret-cast-in-c) – Zabuzard Sep 22 '20 at 17:07
  • In C, a `char` is 1 byte long, so when you say `char[]`, in Java that would be a **`byte[]`**, since Java's `char` is 2 bytes long. --- Since Java by default encodes in big-endian, and you're likely capturing raw data structures, which are likely little-endian, depending on the CPU used, you should use Java's **`ByteBuffer`** to read/write such structures. – Andreas Sep 22 '20 at 17:35

0 Answers0