0

I have two strings one with integer (eg string strInt = "100") and one with hex number (eg string strHex = "0x64"). Whats the quickest/nice/safe way to compare if the values of strInt and strHex are equal(numerically)?

Need to exclude sprintf to prevent buffer overflow Also cant use snprintf - my compiler does not support c++ 11

Thank you all in advance

marcin
  • 125
  • 1
  • 14

2 Answers2

3

Use strtol to convert both to integer and then compare them. You can use strHex.c_str() to convert from c++ string to the c-style string required by strtol.

Example:

long int numHex = strtol(strHex.c_str(),NULL,16); // 16 is the base of the source

long int numInt = strtol(strInt.c_str(),NULL,10);
Jorge Núñez
  • 1,703
  • 12
  • 17
3

I don't see how the sprintf() or snprintf() function would be needed for this.

std::string a = "1337";
std::string b = "0x539";

std::stringstream as;
as.str(a);
std::stringstream bs;
bs.str(b);

int na, nb;
as >> na;
bs >> std::hex >> nb;

std::cout << a << " is " << (na == nb ? "equal" : "not equal") << " to " << b << std::endl;
  • I was thinking about doing it this way - but is this optimal? Seems like lots of memory will be needed to create two stringstreams just to compare two numbers??? – marcin Mar 16 '13 at 12:18
  • 1
    @marcin Look at how the answer suggesting the use of `strtol()` will be massively downvoted soon :P If you have a good optimizing compiler, this should not be an issue, and using C++ code is generally preferred over using C when you're writing something in C++. –  Mar 16 '13 at 12:21
  • @H2CO3 Care to explain why? (I honestly don't know, wouldn't have suggested it otherwise) – Jorge Núñez Mar 16 '13 at 12:24
  • @H2CO3 Not that convinced of using stringstream over strtol... But after all it seems like a more safe option – marcin Mar 16 '13 at 12:47
  • @JorgeNúñez I'm in no way a C++ expert, but people who are much smarter than me generally discourage intermixing C and C++ and suggest to use C++ standard library classes instead. Maybe it's a step towards consistency, abstraction and safety. –  Mar 16 '13 at 12:49
  • 1
    @H2CO3: in this case the safe option is `strtol`. in C++11 that's what the streams use internally. in C++03 they instead used the fscanf family, with **Undefined Behavior** in some cases, in particular for conversion from hex... so, in this case the general guideline of using the higher level functionality, caused selection of decidedly inferior solution: more unsafe, more inefficient, more verbose. :-) only use higher level solution when it saves you work. – Cheers and hth. - Alf Mar 16 '13 at 13:26
  • @Cheersandhth.-Alf That's what I generally say too, but I'm afraid of nearby C++ programmers :P I have been **massively** downvoted several times for having proposed a simpler solution that, however, used C functions instead of `std::` classes. I didn't see the point in it that time, nor do I understand why my answer is better than Jorge's (I'm serious!), practically I'm being self-defensive. –  Mar 16 '13 at 13:33
  • @Cheersandhth.-Alf Thanks a lot, your comment was very helpful to me. I did not explain my question too well but that's exactly the answer I was looking for:) – marcin Mar 16 '13 at 16:48