Converting byte array to unsigned long in C++

11,847

Since buffIn is of type char pointer, when you do *(buffIn) you are just grabbing one character. You have to reinterpret the memory address as an unsigned long pointer and then dereference it.

unsigned long number = *((unsigned long*)buffIn);
Share:
11,847
adrianmcmenamin
Author by

adrianmcmenamin

Updated on June 04, 2022

Comments

  • adrianmcmenamin
    adrianmcmenamin almost 2 years

    I am reading in binary data from a file:

    char* buffIn = new char[8];
    ifstream inFile(path, ifstream::binary);
    inFile.read(buffIn, 8);
    

    I then want to convert the char* read in (as binary) to an unsigned long but I am having problems - I am not quite sure what is going on, but for instance 0x00000000000ACD gets interpreted as 0xFFFFFFFFFFFFCD - I suspect all the 0x00 bytes are causing some sort of problem when converting from char* to unsigned long...

    unsigned long number = *(buffIn);
    

    How do I do this properly?