dan_t Posted July 26, 2009 Share Posted July 26, 2009 I was wondering if someone could tell me why this won't work. <?php echo hex_to_decimal('0xff'); echo "<br>\n"; echo hex_to_decimal('1e5a'); function hex_to_decimal ($hex) { $hex = strtolower($hex); $hex = trim($hex); if (substr($hex, 0, 2) == '0x') $hex = substr($hex, 2); $result = 0; while ($hex != ' ') { $char = substr($hex, 0, 1); if (ord($char) >= ord('a') and ord($char) <= ord('f')) $val = 10 + (ord($char) - ord('a')); else if (ord($char) >= ord('0') and ord($char) <= ord('9')) $val = ord($char) - ord('0'); else return INVALID_HEX_STRING; $result = ($result * 16) + $val; $hex = substr($hex, 1); } return $result; } ?> It just errors about a problem with the invalid_hex_string constant. Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/ Share on other sites More sharing options...
Alex Posted July 26, 2009 Share Posted July 26, 2009 'INVALID_HEX_STRING' isn't defined anywhere. You'd have to define it like.. define("INVALID_HEX_STRING", "Invalid hex string... etc..", true); Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-883380 Share on other sites More sharing options...
dan_t Posted July 27, 2009 Author Share Posted July 27, 2009 <?php echo hex_to_decimal('0xff'); echo "<br>\n"; echo hex_to_decimal('1e5a'); define("INVALID_HEX_STRING", "Invalid hex string", true); function hex_to_decimal ($hex) { $hex = strtolower($hex); $hex = trim($hex); if (substr($hex, 0, 2) == '0x') $hex = substr($hex, 2); $result = 0; while ($hex != ' ') { $char = substr($hex, 0, 1); if (ord($char) >= ord('a') and ord($char) <= ord('f')) $val = 10 + (ord($char) - ord('a')); else if (ord($char) >= ord('0') and ord($char) <= ord('9')) $val = ord($char) - ord('0'); else return INVALID_HEX_STRING; $result = ($result * 16) + $val; $hex = substr($hex, 1); } return $result; } ?> I may have done this wrong, but this did not work either. Is it possible something is not set in the "ini" file? Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-883577 Share on other sites More sharing options...
blueman378 Posted July 27, 2009 Share Posted July 27, 2009 it owuld be easier if you could tell us the exact errors? Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-883588 Share on other sites More sharing options...
Philip Posted July 27, 2009 Share Posted July 27, 2009 The following works: <?php define("INVALID_HEX_STRING", " Invalid hex string", true); echo hex_to_decimal('0xff').' :: '.hexdec('0xff'); echo "<br>\n"; echo hex_to_decimal('1e5a').' :: '.hexdec('1e5a'); function hex_to_decimal ($hex) { $hex = strtolower($hex); $hex = trim($hex); if (substr($hex, 0, 2) == '0x') $hex = substr($hex, 2); $result = 0; while ($hex != '') { $char = substr($hex, 0, 1); if (ord($char) >= ord('a') and ord($char) <= ord('f')) $val = 10 + (ord($char) - ord('a')); else if (ord($char) >= ord('0') and ord($char) <= ord('9')) $val = ord($char) - ord('0'); else return INVALID_HEX_STRING; $result = ($result * 16) + $val; $hex = substr($hex, 1); } return $result; } ?> while ($hex != ' ') should have been while($hex != '') otherwise after it checks the last digit there is nothing left of the string but the loop will run until it finds a space.... which it won't thus returning INVALID HEX STRING Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-883598 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 You do know about hexdec()? Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-883599 Share on other sites More sharing options...
dan_t Posted July 27, 2009 Author Share Posted July 27, 2009 No, sorry never heard of it. I wish I did but I'm not real good with PHP yet. I do not if I ever wiil be, doesn't feel like it. :'( Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-884336 Share on other sites More sharing options...
dan_t Posted July 28, 2009 Author Share Posted July 28, 2009 while ($hex != ' ') should have been while($hex != '') otherwise after it checks the last digit there is nothing left of the string but the loop will run until it finds a space.... which it won't thus returning INVALID HEX STRING Man, I wish I understood that. But you guys are amazing. I could have looked for the next ten years and never found that. Thanks alot. I hope to someday understand some of this. Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-884391 Share on other sites More sharing options...
Philip Posted July 28, 2009 Share Posted July 28, 2009 $hex = substr($hex, 1); That will remove the first character from the string, which is placed at the end of your while loop. So, 1e5a would become e5a after the first pass of the loop. Whenever you run the loop 4 times, you are left with an empty variable, not a variable with the value of a space character. When you had it while ($hex != ' ') it would run that loop until hex was just a ' ' which would never happen. And because it was empty, and your while loop looked for a space, when it came to the if statements, the empty $hex wasn't a character between 'a' and 'f' if (ord($char) >= ord('a') and ord($char) <= ord('f')) , and it wasn't a integer between '0' and '9'.... else if (ord($char) >= ord('0') and ord($char) <= ord('9')) so it fell back to the last else statement - return an invalid hex string. else return INVALID_HEX_STRING; Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-884403 Share on other sites More sharing options...
dan_t Posted July 28, 2009 Author Share Posted July 28, 2009 Thank you I appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/#findComment-884715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.