Sorry, but I don't think so. A "hex" character is a single byte from "0" to "9" and from "A" to "F".
The only part containing ASCII characters is the header, and if it's just a sequence of 4 characters it doesn't matter if ASCII or not, unless they're a hex representation of a 2-bytes integer. With an example we can better understand this section.
The payload length is an uint32 and it's not a problem getting that value for Arduino (it's an "unsigned long", aka "uint_32_t"), you just need to know the bytes order (big-endian or little-endian).
The Payload is just a byte array ("byte[]") buffer, once you know the length you read each received byte into it. If you already know the maximum length possible you can allocate the buffer to that length, if not you should dynamically allocate the appropriate memory space, up to the available RAM memory of your board (which one?).
do you know what can be the the maximum length of your payload?
coz if you want to store/manupilate the payload, depending on how big your payload can get, you may potentially have issues processing if on 'small' memory boards (e.g UNO). please keep that in mind.
as to detecting and receiving the data packet itself that would be pretty standard IMHO
so something like this maybe:
(Compiles, NOT tested!)
At best that is confusing, at worst it's simply incorrect. A single byte is a binary value between 0x00 and 0xFF (0 - 255 decimal). If it's also a '"hex" character is a single byte from "0" to "9" and from "A" to "F"', then it is, in fact, an ASCII representation.
Thanks, it's clearer now. And please forget the "hex characters" term, always speak about "bytes" (here represented by hex values just to let "humans" understand the contents).
The FSM implementation is a good practice and correct. We (I) don't really know if that 4 header bytes are fixed, anyway it looks to me like your code should work.
That's what I meant. The OP wrote the header is "ASCII character" and "the transmission from the device would be hex characters", so my goal was to let the OP be more clear where "hex characters" term is wrong. Then, after the packet example (that's what I asked for), everything becomes clear to us.
So, what is confusing (and perhaps incorrect) is his post, not my reply, and if you reply to me specifying "a hex characteris, in fact, an ASCII representation" is totally superfluous.