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 ...
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 ...
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 ...
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)
{
...
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 };
/* ... */
...
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 ...
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 {
...
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. ...
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]...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
byte-order × 10data-type × 2
serial × 1
esp8266 × 1
arduino-mega × 1
programming × 1
arduino-ide × 1
i2c × 1
library × 1
string × 1
attiny × 1
avr × 1
rfid × 1
code-optimization × 1
float × 1
convert × 1
bit × 1
c-string × 1