Skip to main content
3 votes

4 consecutive bytes in buffer to unsigned long

If they were in the same endian order as a long (little-endian on an Arduino, which they don't appear to be) you could just point to them: uint32_t *l = (uint32_t *)&packetBuffer[40]; Then use *l ...
Majenko's user avatar
  • 106k
3 votes

Problems on convert byte[] to String

If you change str += String(buffer[i]); to str += (char)buffer[i];, it will print the ascii characters to the serial monitor. The following sketch can be used to see the difference in binary compiled ...
VE7JRO's user avatar
  • 2,514
3 votes

Problems on convert byte[] to String

byte and char are the same. If you set 0 as string terminator after last character in the buffer, you get a zero terminated string. If you really must use String, you can create an instance with a ...
Juraj's user avatar
  • 18.3k
3 votes
Accepted

How to map 6 bytes of raw data to long long type?

The trick is to use a union data type and get full control of the mapping. The issue of data representation and endian is now under your control: uint64_t foo(uint8_t* A, uint8_t* B, uint8_t* C) { ...
Mikael Patel's user avatar
  • 7,989
3 votes

How to swap byte order?

You can use an array of byte to save the shifts at all. This solution should be the one with the least clock cycles, and with no additional memory usage. static byte value[4] = { 0 }; /* ... */ ...
the busybee's user avatar
  • 2,466
1 vote
Accepted

Sending multi-byte data over I2C between different processors

There seems to be multiple questions in what you are asking. Here I try to answer specifically this one: if I shift a uint16_t (or any non-trivial datatype) right by 8 bits, and mask out the ...
Edgar Bonet's user avatar
  • 45.2k
1 vote

4 consecutive bytes in buffer to unsigned long

An alternative is to use union and struct, and let the compiler do the work: static inline uint32_t bswap32(uint32_t x) { union { uint32_t x; struct { ...
Mikael Patel's user avatar
  • 7,989
1 vote

Hex/Byte Reversing and Conversion

A standard way to convert individual bytes to a number is to use bit shifts and bitwise OR, as shown in chrisl's answer. When doing this, however, one has to take care not to overflow the bit shifts. ...
Edgar Bonet's user avatar
  • 45.2k
1 vote

How to map 6 bytes of raw data to long long type?

uint64_t foo(uint8_t* A, uint8_t* B, uint8_t* C) { uint64_t aux = 0; aux = A[0]; aux <<= 8; aux |= A[1]; aux <<= 8; aux |= B[0]; aux <<= 8; aux |= B[1]...
gabonator's user avatar
  • 371

Only top scored, non community-wiki answers of a minimum length are eligible