Jump to content
  • Advertisement
Sign in to follow this  
DividedByZero

Why does this return different values in debug/release

This topic is 2895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I found a function on the net that allows a hex formatted string to be converted to the correct hex byte values. But for some reason this is only converting correctly in debug mode and in release the results are different?

Any ideas why this may be so? There are no variables that need to be initialised any further that I can see.

unsigned int htoi(const char *ptr)
{
unsigned int value=0;
char ch=*ptr;

for(;;)
{
if(ch>='0'&&ch<='9')
value=(value<<4)+(ch-'0');
else if(ch>='A'&&ch<='F')
value=(value<<4)+(ch-'A'+10);
else if(ch>='a'&&ch<='f')
value=(value<<4)+(ch-'a'+10);
else
return value;
ch=*(++ptr);
}
}

The reason I want to do this is that I want to convert a Mac address string (12 Chars) to the correct corresponding 6 bytes.

Thanks in advance :)

Share this post


Link to post
Share on other sites
Advertisement
If you're using C++, try it with strings and string streams instead:
#include <sstream>

unsigned ConvertHexToUInt(const std::string& hexstr)
{
unsigned ret = 0;
std::stringstream convert;
convert << std::hex << hexstr;
convert >> ret;
return ret;
}


This performs the same conversion, but in a much safer and more reliable way.


As to why the code posted doesn't work, it depends; can you show the code where you call the htoi function?

Share this post


Link to post
Share on other sites
Oh, one other thing to note - MAC addresses are 48-bit (6 bytes as you noted) which means on most machines they won't fit into a simple unsigned int. You'll need to use a 64-bit integer type (such as long long in C++) to actually get a valid address. Otherwise you'll lose data in the conversion process. In fact, the conversion will fail in the C++ version I posted, which would nail you a lot faster than the silent error generated by the C code you're using.

Share this post


Link to post
Share on other sites
Apart from the result not fitting in a 32bit int as ApochPiQ says, there is also the possibility that *ptr isn't terminated. Since the function doesn't do any bounds checking it would silently incorporate any arbitrary data after your 12 char string if that data was a valid hex char.

Share this post


Link to post
Share on other sites
Thanks for the code snippet ApochPiQ, I'll give that a try.

I have been calling the previous function multiple times to convert 'chunks' over to bytes. So, I shouldn't have any issues with the 'int size' problem.

I'll let you know how it goes :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!