[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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

  1. include <sstream>
  2. include <cassert>
///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:

  1. include <cstring>
  2. include <cassert>
//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 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

Other code snippets



last edited (April 18, 2007) by bilderbikkel, Number of views: 1251, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.