hi .. i was just wonder if there is a function similar to atol, however if will convert a unsigned char to a long ?
hi .. i was just wonder if there is a function similar to atol, however if will convert a unsigned char to a long ?
Something like
long boom;
unsigned char woot=5;
boom=woot;
???
well ... im just tring to convert a unsigned char into a long int after some bit manip ...
void Test::testFunction(ifstream &inFile)
{
long data;
unsigned char buffer[4];
inFile.seekg(6240, ios::beg);
inFile.read(reinterpret_cast<char *>(&buffer[0]), 4);
(buffer[0]<<8)|buffer[3];
(buffer[1]<<8)|buffer[2];
(buffer[2]<<8)|buffer[1];
(buffer[3]<<8)|buffer[0];
data = atol(buffer); // problem here
}
thanks for any help guys
Well you can achieve this by bitwise operators... or a by a strict copying. Remember that a char is 8bits and a long int a 32-bits...
LB0: * Life once school is done
LB1: N <- WakeUp;
LB2: N <- C++_Code;
LB3: N >= Tired : N <- Sleep;
LB4: JMP*-3;
If you change data to this:
long *data;
Then you can do this:
data = (reinterpret_cast<long *> (buffer)); // problem here
Of course, to refer to the value of data, you'll have to dereference it:
cout << *data << endl;
Or create another variable:
Code:long *pdata; long data; . . pdata = (reinterpret_cast<long *> (buffer)); // problem here data = *pdata;
hey guys, thanks for the input
Anways, i tried what you said above swoopy
"data = (reinterpret_cast<long *> (buffer));"
however that did not work. The problem is i need to be able to put the whole contents of "buffer" into one variable of long type.
When shifting bits of an array is there a way to shift everything into one variable ?
ie.
void Test::testFunction(ifstream &inFile)
{
long data;
unsigned char buffer[4];
inFile.seekg(6240, ios::beg);
inFile.read(reinterpret_cast<char *>(&buffer), 4);
data = (buffer[0]<<8)|buffer[3],
(buffer[1]<<8)|buffer[2],
(buffer[2]<<8)|buffer[1],
(buffer[3]<<8)|buffer[0];
}
I try this but only get the first index of the array ? Any help would be appreciated
It should store all four bytes into a long. Run this and see what you get. It should output all f's: ffffffff.
Also, be sure you open your file in binary mode.Code:long data; long *pdata; unsigned char buffer[4] = {0xff,0xff,0xff,0xff}; pdata = (reinterpret_cast<long *> (buffer)); data = *pdata; cout << "data:" << data << endl; cout << "data:" << hex << data << endl;
ifstream inFile(filename,ios::binary);