(C++) HexStrIsInt

August 7, 2019 · View on GitHub

 

 

 

 

 

(C++) HexStrIsInt

 

Check if a hexadecimal std::string can be converted to an integer.

 

There are two options:

  • Option #1: accepts a shorter range, but can also accept negatives
  • Option #2: accepts a much larger range, but cannot accept negatives

 

Code in plain text can be found here (#1) and here (#2).

 

 

 

 

 

Option #1

 


#include <sstream> #include <cassert> //From http://www.richelbilderbeek.nl/CppHexStrIsInt.htm //Checks whether a std::string containg hexadecimal //can be converted to an int. //Returns true if possible, also returning this integer by referencing. //Returns false otherwise, setting the referenced int to zero. bool IsHexInt(const std::string& s, int& rInt) {   std::istringstream i(s);   i >> std::hex;   if (!(i >> rInt))   {     rInt = 0; return false;   }   return true; } int main() {   int temp = -1;   assert(IsHexInt("0",temp) == true);   assert(IsHexInt("9",temp) == true);   assert(IsHexInt("A",temp) == true);   assert(IsHexInt("F",temp) == true);   assert(IsHexInt("-F",temp) == true);   assert(IsHexInt("-F85AE",temp) == true);   assert(IsHexInt("80000000",temp) == true); //Maximal value   assert(IsHexInt("80000001",temp) == false); //Out of range   assert(IsHexInt("G",temp) == false);   assert(IsHexInt("--80000000",temp) == false);   assert(IsHexInt("-800-00000",temp) == true); //Does not detect ill-formed numbers   assert(IsHexInt("80000-000",temp) == true); //Does not detect ill-formed numbers }

 

Note that the std::string cannot have a value higher then 80000000.

 

 

 

 

 

Option #2

 

Suggested by AsmGuru62:

 


#include <cstring> #include <cassert> //From http://www.richelbilderbeek.nl/CppHexStrIsInt.htm //This solution was inspired by AsmGuru62 //Checks if a C-style string only contains hexadecimal characters bool IsAllHex (const char* const must_be_hex) {   char copy_of_param[64];   return(     std::strtok (       std::strcpy(copy_of_param, must_be_hex),       "0123456789ABCDEFabcdef") == 0); } int main() {   assert(IsAllHex("0")==true);   assert(IsAllHex("9")==true);   assert(IsAllHex("A")==true);   assert(IsAllHex("F")==true);   assert(IsAllHex("-F")==false); //Does not accept negatives   assert(IsAllHex("-F85AE")==false); //Does not accept negatives   assert(IsAllHex("FFFFFFFFFFFFFFFFFFFFFFFFFFF")==true);   assert(IsAllHex("G")==false); } //As option #1 also has defects in detecting negatives, you might want to change //the IsAllHex to the one below: //From http://www.richelbilderbeek.nl/CppHexStrIsInt.htm //This solution was inspired by AsmGuru62 //Checks if a C-style string only contains hexadecimal characters //and minuses bool IsAllHex (const char* const must_be_hex) {   char copy_of_param[64];   return(     std::strtok (       std::strcpy(copy_of_param, must_be_hex),       "0123456789ABCDEFabcdef-") == 0); }

 

As option #1 also has defects in detecting negatives, you might want to change the IsAllHex to the one below:

 


//From http://www.richelbilderbeek.nl/CppHexStrIsInt.htm //This solution was inspired by AsmGuru62 //Checks if a C-style string only contains hexadecimal characters //and minuses bool IsAllHex (const char* const must_be_hex) {   char copy_of_param[64];   return(     std::strtok (       std::strcpy(copy_of_param, must_be_hex),         "0123456789ABCDEFabcdef-") == 0); }