I am working on a code that requires me to parse an incoming frame which i receive in form of uint8_t * pointer.
I have written a utility method to fetch mac address from the frame and convert it into uint64_t value for easy calculations.
uint64_t addr = uint64_t(_frm_ptr[0]) << 40 | uint64_t(_frm_ptr[1]) << 32 |
uint64_t(_frm_ptr[2]) << 24 | uint64_t(_frm_ptr[3]) << 16 |
uint64_t(_frm_ptr[4]) << 8 | uint64_t(_frm_ptr[5]);
I need to know that if the source mac address was say "b8:ac:6f:c0:3d:25" on a big endian format, will it be transmitted in the same order ?
How is mac address transmitted on a network ?
Will the above code snippet work on all platforms ?
The bytes in a frame are in a fixed order (big-endian as @Caleb points out), independent of endianness of the host computer.
Since you're copying the bytes one-by-one this will always work. There's only a problem when you would use multi-byte assignments or memcpy
's from the frame data into short
, int
, .... Have a look at ntohs()
and friends that convert network-to-host and vice versa.