C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Verify if String has valid hex values in C++ Posted by drajwani on 18 Apr 2007 at 3:39 PM
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
Report
Re: Verify if String has valid hex values in C++ Posted by dartsman on 18 Apr 2007 at 4:05 PM
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
:

Report
Re: Verify if String has valid hex values in C++ Posted by drajwani on 18 Apr 2007 at 4:36 PM
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
: :
:
:

Report
Re: Verify if String has valid hex values in C++ Posted by AsmGuru62 on 18 Apr 2007 at 6:17 PM
Here you go!
You just have to make sure that parameter does not exceed 63 characters.

bool IsAllHex (char* must_be_hex)
{
  char copy_of_param [64];

  return (
    strtok (
      strcpy (copy_of_param, must_be_hex),
      "0123456789ABCDEFabcdef") == NULL);
}

Report
Re: Verify if String has valid hex values in C++ Posted by bilderbikkel on 18 Apr 2007 at 11:32 PM
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

Report
Re: Verify if String has valid hex values in C++ Posted by drajwani on 19 Apr 2007 at 10:28 AM
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

: Here you go!
: You just have to make sure that parameter does not exceed 63 characters.
:

:
: bool IsAllHex (char* must_be_hex)
: {
:   char copy_of_param [64];
: 
:   return (
:     strtok (
:       strcpy (copy_of_param, must_be_hex),
:       "0123456789ABCDEFabcdef") == NULL);
: }
: 

:

Report
Re: Verify if String has valid hex values in C++ Posted by alupiv on 14 Jun 2010 at 10:58 PM
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...

Report
Re: Verify if String has valid hex values in C++ Posted by bilderbikkel on 18 Apr 2007 at 11:11 PM
: 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]]:

#include <sstream>
#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("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);
}


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

See ya


bilderbikkel

Report
This post has been deleted. Posted by dsd12345 on 24 Jun 2010 at 4:09 AM
This post has been deleted.
Report
This post has been deleted. Posted by dsd12345 on 24 Jun 2010 at 4:11 AM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - 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 our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.