converting byte array to long

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jov
    New Member
    • Feb 2008
    • 1

    converting byte array to long

    i would like to copy the contents of a byte array to a long variable. my code looks like this:

    long key = 0;
    for (i = 0; i < 8; i++) {
    key <<= 8;
    key |= (long)(buffer[i]); //<- this line causes the problem
    }

    however, when i run the program, there is an error message saying "unable to handle exception". what may have cause this problem?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Good trick. An 8-element byte array spans 64 bits and a long is probably 32 bits.

    Even if the long is 64 bits, is your machine big endian or little endian and how is your byte array structured ??

    Exactly what are you trying to do??

    Comment

    Working...