Skip to content

Little endian support #3

Open
Open

Description

@yisonPylkita

Hi :)

Thank you for such a simple yet working library.

I noticed that this library only supports big endianness. I want to use it on x86 system and to use this library I would have to reverse bytes first.

Can you add functions like

template<class T>
T to_variable_from_le(const uint8_t* block)

and

template<class T>
T to_variable_from_be(const uint8_t* block)

Activity

  1. yisonPylkita commented on Oct 17, 2018

    @yisonPylkita
    Author

    Or maybe a default parameter

    #ifdef _WIN32
    #define __LITTLE_ENDIAN    1234
    #define BYTE_ORDER __LITTLE_ENDIAN
    #endif
    
    enum class ByteOrder : uint32_t {
        little_endian = __LITTLE_ENDIAN,
        big_endian = __BIG_ENDIAN,
    };
    
    template<class T>
    T to_variable(const uint8_t* block, ByteOrder byte_order = static_cast<ByteOrder>(BYTE_ORDER))
  2. self-assigned this
    on Oct 17, 2018
  3. SloCompTech commented on Oct 17, 2018

    @SloCompTech
    Owner

    Hi, thanks for support,
    yes that would be a improvement, would you be able to improve library by adding metioned functions, because I'm currently lacking time to do it ?

  4. yisonPylkita commented on Oct 18, 2018

    @yisonPylkita
    Author

    Yes, I will create a MR

    For the project I am working on I ended up with this - https://godbolt.org/z/Tz7Wvg

    uint16_t load_uint16_from_le(const uint8_t *raw_data)
    {
        return ((uint16_t)(raw_data[1]) << 8) | (uint16_t)raw_data[0];
    }
    
    uint16_t load_uint16_from_be(const uint8_t *raw_data)
    {
        return ((uint16_t) (raw_data[0]) << 8) | (uint16_t) raw_data[1];
    }
    
    uint32_t load_uint32_from_le(const uint8_t *raw_data)
    {
        return (uint32_t)(raw_data[3]) << 24 | (uint32_t)raw_data[2] << 16 | (uint32_t)raw_data[1] << 8 | (uint32_t)raw_data[0];
    }
    
    uint32_t load_uint32_from_be(const uint8_t *raw_data)
    {
        return ((uint32_t)(raw_data[0]) << 24) | (uint32_t)raw_data[1] << 16 | (uint32_t)raw_data[2] << 8 | (uint32_t)raw_data[3];
    }

    I will refactor this code to fit your library. So ByteConvert_cpp will support:

    • conversions from little/big endian on little endian system
    • conversions from big endian on big endian system
  5. SloCompTech commented on Oct 18, 2018

    @SloCompTech
    Owner

    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions