Reversed byte order

Hey everyone, this is not something I've had to deal with before, so I'm not even sure what to search for. If there's already a post, could you please point me towards it? If not, here's my situation.

I'm working with an ESP8266 powered remote that sends out a signal with ESP-Now, trying to decode it, and one of the things it sends out is 4 bytes as an incremental number.

As I'm decoding the signal I've found that the bytes are in this order

Example of first packet sent: 00100111 00000010 00000000 00000000

Example of second packet sent: 00101000 00000010 00000000 00000000

If when decoding this I read it as a long it would be:

First: 00100111000000100000000000000000 which is 27,020,000 Second: 00101000000000100000000000000000 which is 28,020,000

However, if you reverse the bytes (not each but) you get:

First: 00000000000000000000001000100111 which is 551 Second: 00000000000000000000001000101000 which is 552

So, my questions are:

Is there any different data type that would read this in reverse order? If not, is there any way to reverse the order?

Also, for re-creating the signal (to make my own version of the remote), I would need to take a regular long and reverse it. how would I do that?

I know it's not a lot to work with, but I don't want to give away too much of the program until I'm sure it works. If there is any other details needed to help me let me know.

Thanks in advance!

If your ESP8266 has the ESPRESSIF freeRTOS extension you can use the RingBuffer to do what you want. The ESP8266 with RTOS API is what you'd be looking for; if your ESP8266 has the things.

Put the 4 bytes into an uint8_t array, reversing their order. Then, memcpy the array into a variable of type uint32_t.

How would I know for sure if it has the freeRTOS extension? Or can it be added? Is this not just a library I can add to arduino?

gfvalvo: Put the 4 bytes into an uint8_t array, reversing their order. Then, memcpy the array into a uint32_t.

That sounds like an easy enough option. I didn't think about memcpy.

It isn’t “Bit Order”, That’s “Byte Order” or “Endianness”.

You can swap order use following macro.

#define BYTESWAP32(z) ((uint32_t)((z&0xFF)<<24|((z>>8)&0xFF)<<16|((z>>16)&0xFF)<<8|((z>>24)&0xFF)))

VARIABLE = BYTESWAP32(VARIABLE);

chrisknightley:
It isn’t “Bit Order”, That’s “Byte Order” or “Endianness”.
Endianness - Wikipedia

You can swap order use following macro.

#define BYTESWAP32(z) ((uint32_t)((z&0xFF)<<24|((z>>8)&0xFF)<<16|((z>>16)&0xFF)<<8|((z>>24)&0xFF)))

VARIABLE = BYTESWAP32(VARIABLE);

You are absolutely correct. In my message I typed byte, but in the topic I accidentally put bit. My apologies. I edited the topic to correct this.

That’s perfect! That’s exactly what I was looking for! I’m still new to some of this and wasn’t quite sure what to look for.

kb1sph: How would I know for sure if it has the freeRTOS extension? Or can it be added? Is this not just a library I can add to arduino?

I do not use ESP8266's so I am not sure how you can tell. I use ESP32's, they include freeRTOS automatically.