This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

BinaryX : X indicates the length in bytes for the binary.

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.

Parents
  • How about:

    unsigned char Binary5[5];   // Binary5 is 5 bytes long binary data
    

    "I'm in project and in specs"

    You're in two places at once? Clever!

    "Is this a mistake of document writer? I have no idea."

    You have the "document" in front of you.
    How do you expect anyone who doesn't even know what "document" you're talking about to have any better idea??!

Reply
  • How about:

    unsigned char Binary5[5];   // Binary5 is 5 bytes long binary data
    

    "I'm in project and in specs"

    You're in two places at once? Clever!

    "Is this a mistake of document writer? I have no idea."

    You have the "document" in front of you.
    How do you expect anyone who doesn't even know what "document" you're talking about to have any better idea??!

Children
  • "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.

  • Thank for your reply.
    I'm not a english speaker, so I made a mistake in my writing.
    Anyway thanks again.

  • @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.
    ---------------------------------------

  • Google will give you references to big-number libraries.

    Depending on what evaluations you need to do, it is also quite simple to write your own integer code for handling numbers larger than what the target hardware can do natively.

    You can do the same thing you do with pen and paper in the decimal number system, but instead of having 10 digits 0..9, you can use 256 digits (0..255) or 65536 digits (0..65535) or whatever you feel works best.

    It is preferable to select a number base where the processor can multiply two numbers and store the product, so if the processor only have 8x8 multiply giving 16-bit results, you shouldn't select a number base larger than 2^8, i.e. don't select "digits" larger than what fits in one byte.

    Depending on what big-num library you use, you will have to write code that picks up the data from the serial port, and imports/converts the data into the format used by the BN library. Same for outputting any results.