user47958: I'm trying to solve some problems for a contest, and I have a problem with it. I have to implement a function that has 3 arguments like this:
void foo(unsigned char* A, unsigned char* B, unsigned char* C);

And what comes in, is 6 bytes of data. I have to convert these 6 bytes of data to integer data (maybe long long). The problem is, I don't know how to map the bytes in the right order.
I'm using an Arduino Uno board.
Please help me with it.

user45984: 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];
    aux <<= 8;
    aux |= C[0];
    aux <<= 8;
    aux |= C[1];
    return aux;
}

following sample code:
  uint8_t a[] = { 0x12, 0x34 };
  uint8_t b[] = { 0x56, 0x78 };
  uint8_t c[] = { 0x9a, 0xbc };

  uint64_t result = foo(a, b, c);

  std::cout << std::hex << result;

returns 123456789abc, try the code here, excluding the std::out part, the same code will run on arduino without any problem

user13432: 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)
{
  union {
    uint64_t X;
    struct {
      uint8_t A[2];
      uint8_t B[2];
      uint8_t C[2];
      uint8_t D[2];
    };
  } map;
#if defined(MSB_ORDER)
  for (int i = 0, j = 1; i < 2; i++, j--) {
    map.A[i] = 0;
    map.B[i] = C[j];
    map.C[i] = B[j];
    map.D[i] = A[j];
  }
#else
  for (int i = 0; i < 2; i++) {
    map.A[i] = A[i];
    map.B[i] = B[i];
    map.C[i] = C[i];
    map.D[i] = 0;
  }
#endif
  return map.X;
}

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  uint8_t a[] = { 0x12, 0x34 };
  uint8_t b[] = { 0x56, 0x78 };
  uint8_t c[] = { 0x9a, 0xbc };
  uint64_t res = foo(a, b, c);
  Serial.print((uint32_t) (res >> 32), HEX);
  Serial.print((uint32_t) res, HEX);
}

void loop()
{
}

Cheers!

47958: <p>I'm trying to solve some problems for a contest, and I have a problem with it. I have to implement a function that has 3 arguments like this:</p>

<pre><code>void foo(unsigned char* A, unsigned char* B, unsigned char* C);
</code></pre>

<p>And what comes in, is 6 bytes of data. I have to convert these 6 bytes of data to integer data (maybe long long). The problem is, I don't know how to map the bytes in the right order.</p>

<p>I'm using an Arduino Uno board.</p>

<p>Please help me with it.</p>
 45984: <pre><code>uint64_t foo(uint8_t* A, uint8_t* B, uint8_t* C)
{
    uint64_t aux = 0;
    aux = A[0];
    aux &lt;&lt;= 8;
    aux |= A[1];
    aux &lt;&lt;= 8;
    aux |= B[0];
    aux &lt;&lt;= 8;
    aux |= B[1];
    aux &lt;&lt;= 8;
    aux |= C[0];
    aux &lt;&lt;= 8;
    aux |= C[1];
    return aux;
}
</code></pre>

<p>following sample code:</p>

<pre><code>  uint8_t a[] = { 0x12, 0x34 };
  uint8_t b[] = { 0x56, 0x78 };
  uint8_t c[] = { 0x9a, 0xbc };

  uint64_t result = foo(a, b, c);

  std::cout &lt;&lt; std::hex &lt;&lt; result;
</code></pre>

<p>returns <code>123456789abc</code>, try <a href="https://onlinegdb.com/S15ZvIfmQ" rel="nofollow noreferrer">the code here</a>, excluding the std::out part, the same code will run on arduino without any problem</p>
 13432: <p>The trick is to use a <code>union</code> data type and get full control of the mapping. The issue of data representation and endian is now under your control: </p>

<pre><code>uint64_t foo(uint8_t* A, uint8_t* B, uint8_t* C)
{
  union {
    uint64_t X;
    struct {
      uint8_t A[2];
      uint8_t B[2];
      uint8_t C[2];
      uint8_t D[2];
    };
  } map;
#if defined(MSB_ORDER)
  for (int i = 0, j = 1; i &lt; 2; i++, j--) {
    map.A[i] = 0;
    map.B[i] = C[j];
    map.C[i] = B[j];
    map.D[i] = A[j];
  }
#else
  for (int i = 0; i &lt; 2; i++) {
    map.A[i] = A[i];
    map.B[i] = B[i];
    map.C[i] = C[i];
    map.D[i] = 0;
  }
#endif
  return map.X;
}

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  uint8_t a[] = { 0x12, 0x34 };
  uint8_t b[] = { 0x56, 0x78 };
  uint8_t c[] = { 0x9a, 0xbc };
  uint64_t res = foo(a, b, c);
  Serial.print((uint32_t) (res &gt;&gt; 32), HEX);
  Serial.print((uint32_t) res, HEX);
}

void loop()
{
}
</code></pre>

<p>Cheers!</p>
