Search code examples
c++castingunsigned-charlong-long

Unsigned char array casting to long long


I'm not sure if this is correct, I have tested it and seems that some bytes are off... Basically, I have the following:

unsigned char szBuffer[1024] = {0};
long long nValue = 1334553536;
memcpy(szBuffer, (char*)&nValue, sizeof(long long));

//

long long nNewValue = reinterpret_cast<long long>(szBuffer);
printf(nNewValue); //prints out a smaller number than previously stated

Would anyone mind pointing out where I went wrong? Thank you.


Solution

  • You're setting nNewValue to the address of szBuffer, instead of reading data from that address. Use:

    long long nNewValue = *reinterpret_cast<long long*>(szBuffer);