Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little endian support #3

Open
yisonPylkita opened this issue Oct 17, 2018 · 4 comments
Open

Little endian support #3

yisonPylkita opened this issue Oct 17, 2018 · 4 comments
Assignees

Comments

@yisonPylkita
Copy link

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)
@yisonPylkita
Copy link
Author

yisonPylkita commented Oct 17, 2018

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))

@SloCompTech SloCompTech self-assigned this Oct 17, 2018
@SloCompTech
Copy link
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 ?

@yisonPylkita
Copy link
Author

yisonPylkita commented Oct 18, 2018

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

@SloCompTech
Copy link
Owner

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants