86348

Convert array of 8 bytes to signed long in C++

I have an array of 8 bytes and I'm trying to convert it to a signed long in C++, and can't seem to figure it out. From what I could tell long ints are only 4 bytes, can anybody provide some information on this? Is it going to matter if it is 32 or 64 bit?

Answer1:

You probably should use a int64_t which is guaranteeed to be 8 bytes long.

You don't state how your data is represented (its endianness) into your array but you might use reinterpret_cast<> or even better: use shift operations to "build" your integer.

Something like:

unsigned char array[8] = { /* Some values here */ }; uint64_t value = static_cast<uint64_t>(array[0]) | static_cast<uint64_t>(array[1]) << 8 | static_cast<uint64_t>(array[2]) << 16 | static_cast<uint64_t>(array[3]) << 24 | static_cast<uint64_t>(array[4]) << 32 | static_cast<uint64_t>(array[5]) << 40 | static_cast<uint64_t>(array[6]) << 48 | static_cast<uint64_t>(array[7]) << 56;

Answer2:

Another way of conversion between data types, which I find convenient in some cases, is to use the union data type, which allows you to access the same memory portion as different data types. Of course all other remarks regarding endianness, size of data-types etc. still hold.

For example:

union bytes { unsigned char c[8]; uint64_t l; } myb; myb.c[0] = 0; myb.c[1] = 1; myb.c[2] = 0; myb.c[3] = 0; myb.c[4] = 0; myb.c[5] = 0; myb.c[6] = 0; myb.c[7] = 0; cout << "value of myb.l: " << myb.l << "\n";

Answer3:

Only C99 has types guaranteed to hold 64 bits of information - long long and int*64_t. For C++, you should look for a BigInt class/library. Or, as has been suggested, roll your own class using two longs.

Answer4:

Some long integers are 8 bytes, some are 4. The 8 byte variant usually exists only on 64-bit systems. Check this page if you want to get an idea.

Of course, we don't know what it is in your case, so you'd have to post more details.

To be on the safe side, you could decide to put the 8 bytes into 2 long integers anyway. That way, it will always work. In the worst case you'd be wasting half your storage space.

Recommend

  • Parsing Integer and Float Values of a Text File with sscanf
  • fseek on a position beyond EOF does not trigger EOF using feof, how come?
  • In C, can you have a 128-bit bitfield [duplicate]
  • Is it safe to combine sizeof and placement new?
  • Template based Subject Observer pattern - Should I use static_cast or dynamic_cast
  • How to programmatically detect if a bitmap has alpha channel?
  • QModelIndex becomes invalid when removing rows
  • How to use make_transform_iterator() with counting_iterator and execution_policy in Thrust?
  • PowerShell execution policy subverted by powershell.exe parameter?
  • How to find data from last week in MySQL
  • How to format code on aptana 3?
  • How to solve “undefined reference to function” error?
  • Count New Lines in Text File
  • Need code translation from VB to C#
  • Compare struct to a constant in C
  • Python cosine function precision [duplicate]
  • Plotting densities in R
  • vectorized indexing/slicing in numpy/scipy?
  • Rest Services conventions
  • Z3: Convert between FP and BitVector?
  • Appending Character to Character Array In C
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Counter field in MS Access, how to generate?
  • AES padding and writing the ciphertext to a disk file
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Join two tables and save into third-sql
  • Linker errors when using intrinsic function via function pointer
  • Understanding cpu registers
  • -fvisibility=hidden not passed by compiler for Debug builds
  • LevelDB C iterator
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF