Hi, Forum.
Does anyone know how to define and handle odd bytes long binary data. For example, Binary5 is 5 bytes long binary data.
I'm in project and in specs, there is this type of data. Is this a mistake of document writer? I have no idea.
I'd like to hear you guys. Thanks.
"Does anyone know how to define and handle odd bytes long binary data."
You can define it anyway you like.
typedef struct { uint32_t part_1; uint8_t part_2; } uint40_t;
typedef struct { uint8_t part_1; uint32_t part_2; } uint40_t;
typedef struct { uint8_t part_1; uint16_t part_2; uint16_t part_3; } uint40_t;
uint8_t parts[5];
...
In reality, you have more than one thing to consider.
1) Where do you get the data from? Serial channel or memory? Both situations requires you to consider the byte order of the 5 individual bytes.
2) Where are you going to send/store them? Both situations requires you to consider the byte order of the 5 individual bytes.
3) Are you going to perform arithmetic? If your processsor/compiler supports uint64_t, it may be elegant to convert your 40-bit values into 64-bit integers, perform the evaluation and then store the result or process potential overflow. Or you may use a big-number library (or own code) to implement add/sub/mul/div/mod/... on your 40-bit numbers.
There really are no generic "how to define and handle" here. You need to consider the specifics of your specification and your target hardware and your compiler and whatever other resources you may have available.
@Per Westermark
Thank you very much.
I'm a new man in this forum and Keil especially serial communcation programming in C. I appreciate if you could tell me where can I get an example for those things to consider? Basically, I understand byte oder.
1) It'll be received from serial channel. 2) And It'll be sent via serial channel and needed to be stored in a safe memory area. 3) The processor support 32-bits. It seems that I have to implement add/sub/mul/div/mod/... on my 40-bit numbers.
****** you may use a big number library -> Can you tell me about a big number library?
Cheers. --------------------------------------- Are there any mistakes in this message? Can't you understand what does it mean? I'm not a english speaker, so I need your kind understand. ---------------------------------------