Jump to content

Recommended Posts

I was wondering if someone could tell me why this won't work.

 

:confused:<?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.

Link to comment
https://forums.phpfreaks.com/topic/167519-someting-wrong-with-script/
Share on other sites

<?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?

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

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.

$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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.