Ive tearing what little hair I have left out over this one and not sure where else to turn so created account here (after picking up so many tips over the years).
I am reading tag/data pairs from a binary file and all working fine except for values with tag data value of 242.
tag/data pairs are stored in vector of simple structs as follows:
1 2 3 4 5
struct TagData
{
int Tag;
int Data;
};
these are parsed and read in in pairs by loop that calls my GetNextTag function as follows:
bool DLogg_DataSet::GetNextTag (ifstream &DLF, int &tag, int &data)
{
char buffer_4b[4];
char buffer_1b[1];
DLF.read ((char*)&buffer_1b, 1);
tag = buffer_1b[0];
DLF.read ((char*)&buffer_4b, 4);
data = *(int*) buffer_4b;
//check for failures...
if (DLF.fail()) returnfalse;
if (DLF.eof()) returnfalse;
//thats all we check for, maybe more to check...
returntrue;
}
Now the issue I have is as follows
tag = buffer_1b[0]; ///TODO: this is reading in hec 0xF2 to int as -14 not 242...
I have run out of ideas and works for other tags but this is the highest number read in so must have something to do with this.
Throwing it out to the larger audience to see if there are any thoughts or glimmers of insperation?
You don't need to have temporary buffers for this. Just read to your data directly:
1 2 3 4 5 6 7 8 9 10 11 12 13
// note this only works for little endian systems -- but you're already doing endian specific stuff
// so I assume it doesn't matter
bool DLogg_DataSet::GetNextTag (ifstream &DLF, int &tag, int &data)
{
tag = 0; // clear high bytes of tag
DLF.read( (char*)(&tag), 1 ); // read it into tag directly. No need to make a separate buffer
// assumes little endian
DLF.read( (char*)(&data), 4 ); // assumes sizeof(int) == 4, assumes file saved with system endianness
// ... check for errors or whatever
}
Peter87, Had tried this as thought it looked like the sign bit might be screwing things up but didn't work. However... will retry when back at PC (tmrw!!!).
Albtross, 1. Aide memoir and personal (possibly incorrect) style!! 2. Wanted it cast and stored as an int for use elsewhere in program.
Disch, Thanks that looks more compact. file not saved with system endianness though. Always little endian as file comes from external microprocessor.
Any more suggestions as to why F2 giving -14 and not 242???
-128+64+32+16+0+0+2+0=-14.... so must be something to do with this.
Thanks for the feedback and will have a mess around with it later and see if it is solved. Looks like peter has it but sure this didnt work for me and so dismissed it....