How to convert unsigned char array to unsigned long long?

let's say that I have 8 unsigned char that I want to convert to unsigned long long.

for example, if all char is 0xFF, unsigned long long will be 0xFFFFFFFFFFFFFFFFFF.

What is the most efficient way to do this using C or C ++?

+4
source share
3 answers

For example:

unsigned char buffer[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
unsigned long long target;
memcpy(&target, buffer, sizeof target);

Please note that if not all elements bufferhave the same value, the result will depend on the byte order (low-daily and large-endian).

, unsigned long long 8 . , . ( , 8 , . unsigned char - 1 .)

+4

memcpy

  unsigned char buffer[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  unsigned long long l = 0;
  for (int i = 0; i < 8; ++i) {
    l = l | ((unsigned long long)buffer[i] << (8 * i));
  }

, .

+4

, . , . , , , endianness , .

++:

#include <iostream>
#include <cstring>

using namespace std;

// Assuming ca is at least 8 bytes, the size of long long, interpret the
//    first 8 bytes as long long.
// differentEndian == true means the endianness of the machine is
//    different from the representation in ca.
long long getLongLong( unsigned char * ca, bool differentEndian )
{
   long long retVal;

   if (differentEndian)
   {
       for (int i = 0; i < 4; i++)
       {
           unsigned char _tmpCh = ca[i];
           ca[i] = ca[7-i];
           ca[7-i] = _tmpCh;
       }
   }
   retVal = *reinterpret_cast<unsigned long long *>(ca);

   return retVal;
}

int main()
{
   unsigned char cArray[] = {0xff, 0x1, 0x70, 0x2, 0x61, 0x3, 0x52, 0x4};

   unsigned long long ll = getLongLong( cArray, false );
   cout << "Result for same endian: " << hex << ll << " or " << dec << ll << endl;

   ll = getLongLong( cArray, true );
   cout << "Result for different endian: " << hex << ll << " or " << dec << ll << endl;

   return 0;
}

#include <stdio.h>
#include <string.h>

// Assuming ca is at least 8 bytes, the size of long long, interpret the
//    first 8 bytes as long long.
// differentEndian != 0 means the endianness of the machine is
//    different from the representation in ca.
long long getLongLong( unsigned char * ca, int differentEndian )
{
   long long retVal;

   if (differentEndian)
   {
       int i;
       for (i = 0; i < 4; i++)
       {
           unsigned char _tmpCh = ca[i];
           ca[i] = ca[7-i];
           ca[7-i] = _tmpCh;
       }
   }
   memcpy( &retVal, ca, sizeof(long long));

   return retVal;
}

int main()
{
   unsigned char cArray[] = {0xff, 0x1, 0x70, 0x2, 0x61, 0x3, 0x52, 0x4};

   unsigned long long ll = getLongLong( cArray, 0 );
   printf("Result for same endian: %llx or %llu\n", ll, ll);

   ll = getLongLong( cArray, 1 );
   printf("Result for different endian: %llx or %llu\n", ll, ll);

   return 0;
}

:

Result for same endian: 4520361027001ff or 311315039429591551
Result for different endian: ff01700261035204 or 1837509111016818739
0

Source: https://habr.com/ru/post/1624201/


All Articles