Verify if String has valid hex values in C++ - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Verify if String has valid hex values in C++

drajwanidrajwani Posts: 3Member
Hi,

I have a String that gets set to a hex value in C++.
i need to be able to verify that the value contains hex characters only.

what is the best way to check this.

so Example

string test=argv[1];

i need to verify that test contains only valid hex characters

Thanks

Comments

  • dartsmandartsman Posts: 88Member
    Hey,

    Have a C style string (character array) of the various hex values and check your new string, character by character that it matches any one within the various hex values string.

    Just some pseudo code... don't know if this would actually compile, but should give you enough of an idea to get started :)


    char hexValues[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f'};
    int hexLength = strlen(hexValues);

    bool isHex = false;

    char *test = argv[1];
    int testLength = strlen(test);

    for (int index = 0; index < testLength; ++index)
    {
    isHex = false;

    // check if the character in test is within hex values array..
    for (int hexIndex = 0; hexIndex < hexLength; ++hexIndex)
    {
    if (test[index] == hexValues[hexIndex])
    {
    // found it within the hex values array
    isHex = true;
    // no need to keep looping in the hexIndex loop, so break..
    break;
    }
    }

    // if this one wasn't a hex value, the obviously the rest of the string isn't...
    if (!isHex)
    break;
    }

    // show some output
    if (isHex)
    std::cout << "Value is hex!" << std::endl;
    else
    std::cout << "Value isn't hex!" << std::endl;


    : Hi,
    :
    : I have a String that gets set to a hex value in C++.
    : i need to be able to verify that the value contains hex characters only.
    :
    : what is the best way to check this.
    :
    : so Example
    :
    : string test=argv[1];
    :
    : i need to verify that test contains only valid hex characters
    :
    : Thanks
    :

  • drajwanidrajwani Posts: 3Member
    I was wondering if there was some existing functions maybe in the string library or any other popular one that would make this check easier, rather than a double for loop

    I am just used to Java and new to C++

    Thanks

    : Hey,
    :
    : Have a C style string (character array) of the various hex values and check your new string, character by character that it matches any one within the various hex values string.
    :
    : Just some pseudo code... don't know if this would actually compile, but should give you enough of an idea to get started :)
    :
    :
    : char hexValues[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f'};
    : int hexLength = strlen(hexValues);
    :
    : bool isHex = false;
    :
    : char *test = argv[1];
    : int testLength = strlen(test);
    :
    : for (int index = 0; index < testLength; ++index)
    : {
    : isHex = false;
    :
    : // check if the character in test is within hex values array..
    : for (int hexIndex = 0; hexIndex < hexLength; ++hexIndex)
    : {
    : if (test[index] == hexValues[hexIndex])
    : {
    : // found it within the hex values array
    : isHex = true;
    : // no need to keep looping in the hexIndex loop, so break..
    : break;
    : }
    : }
    :
    : // if this one wasn't a hex value, the obviously the rest of the string isn't...
    : if (!isHex)
    : break;
    : }
    :
    : // show some output
    : if (isHex)
    : std::cout << "Value is hex!" << std::endl;
    : else
    : std::cout << "Value isn't hex!" << std::endl;
    :
    :
    : : Hi,
    : :
    : : I have a String that gets set to a hex value in C++.
    : : i need to be able to verify that the value contains hex characters only.
    : :
    : : what is the best way to check this.
    : :
    : : so Example
    : :
    : : string test=argv[1];
    : :
    : : i need to verify that test contains only valid hex characters
    : :
    : : Thanks
    : :
    :
    :

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]Here you go!
    You just have to make sure that parameter does not exceed 63 characters.
    [/blue]
    [code]
    bool IsAllHex (char* must_be_hex)
    {
    char copy_of_param [64];

    return (
    strtok (
    strcpy (copy_of_param, must_be_hex),
    "0123456789ABCDEFabcdef") == NULL);
    }
    [/code]
  • bilderbikkelbilderbikkel Posts: 754Member
    : I was wondering if there was some existing functions maybe in the string library or any other popular one that would make this check easier, rather than a double for loop

    From [[http://www.codepedia.com/1/CppHexStrIsInt]]:

    [code]
    #include
    #include

    ///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("B",temp) == true);
    assert(IsHexInt("C",temp) == true);
    assert(IsHexInt("D",temp) == true);
    assert(IsHexInt("E",temp) == true);
    assert(IsHexInt("F",temp) == true);
    assert(IsHexInt("-F",temp) == true);
    assert(IsHexInt("-F85AE",temp) == true);
    assert(IsHexInt("-F85AE",temp) == true);
    assert(IsHexInt("FFFFFFF",temp) == true);
    assert(IsHexInt("80000000",temp) == true); // MAX
    assert(IsHexInt("80000001",temp) == false);
    assert(IsHexInt("G",temp) == false);

    assert(IsHexInt("G",temp) == false);
    }
    [/code]

    Although I noted that the range is not too big, the max is the std::string [b]80000000[/b]. For bigger ranges, I'd use AsmGuru's solution.

    See ya


    bilderbikkel

  • bilderbikkelbilderbikkel Posts: 754Member
    Hi AsmGuru,

    Great code!

    I put it on the CodePedia (http://www.codepedia.com/1/CppHexStrIsInt) with your name next to.

    I hope you appreciate it.

    See ya,
    bilderbikkel

  • drajwanidrajwani Posts: 3Member
    Awesome dude, It worked, of course i am using VC++, so i had use strtok_s, examples in Visual Studio for strtok_s is pretty good.

    Thanks

    : [blue]Here you go!
    : You just have to make sure that parameter does not exceed 63 characters.
    : [/blue]
    : [code]
    : bool IsAllHex (char* must_be_hex)
    : {
    : char copy_of_param [64];
    :
    : return (
    : strtok (
    : strcpy (copy_of_param, must_be_hex),
    : "0123456789ABCDEFabcdef") == NULL);
    : }
    : [/code]
    :

  • alupivalupiv Posts: 1Member
    Hi,

    The code works fine. but i can't understand how this is working where ever i search about strtok found it search for the s2 in s1 if success it return address to the rest string of s1 if not NULL..

    i couldn't apply that here..

    Please can u tell how exactly it will work...

  • dsd12345dsd12345 Posts: 3Member
    This post has been deleted.
  • dsd12345dsd12345 Posts: 3Member
    This post has been deleted.
Sign In or Register to comment.