Hey everybody...
I am taking a string full of hex values and putting them into an array of unsigned shorts. However I want to be able to check to see if the value is actually a hex value (IE input foo would return error). Right now if there is an invalid point it just sits on itself and reads the same value over and over again because it can't read that string since it is not a hex...
So basically I need it to check to make sure the value is indeed actually hex, or it won't even read it in. What is the best way to do this? I thought of reading in the value as a string first, then reading that into a hex value and see if it takes, but that seems excessive...
Code:
int value, i;
unsigned short *pack = new unsigned short [ count+5 ];
stringstream dataStream ( data );
// Arr spots 0-2 held for other purposes
for ( i = 3; i < cmd.count+4; i++) {
dataStream >> hex >> value;
// Check validity of the value
if ( value > MAX_DATA_VALUE || value < 0 ) {
cout << "ERR: Data value " << hex << value << " is out of range" << endl;
pack[0] = INVALID_VALUE;
return pack;
}
else {
pack[i]=value;
}
} // End For
return pack;
Thanks!