[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppHexStrIsInt
Note that the std::string cannot have a value higher then 80000000.
As option #1 also has defects in detecting negatives, you might want to change the funtion IsAllHex to the one below:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppHexStrIsInt
(C++) 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
Option #1
///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 }
- include <sstream>
- include <cassert>
Note that the std::string cannot have a value higher then 80000000.
Option #2
Suggested 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); }
- include <cstring>
- include <cassert>
As option #1 also has defects in detecting negatives, you might want to change the funtion IsAllHex to the one below:
//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);
}
Code links
- #include
- assert
- cassert (header file)
- cstring (header file)
- include, #include
- int
- ?istringstream, std::istringstream
- main
- return
- sstream (header file)
- std::strtok
- std::strcpy
- strtok, std::strtok
- strcpy, std::strcpy
Other code snippets
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
