isHex() and isOcta() functions

suswato picture suswato · Oct 28, 2012 · Viewed 7.6k times · Source

I have Two Functions. IsOcta and isHex. Can not seem to make isHex work properly.
Issue in isHex() is that it can not omit 'x' notation of the original string x23.

Original hex srting can also be D1CE. So adding x and then comparing wont do.

Is there any correct solution to the isHex function. Also is isOcta correct?

function isHex($string){
    (int) $x=hexdec("$string");     // Input must be a String and hexdec returns NUMBER
    $y=dechex($x);          // Must be a Number and dechex returns STRING
    echo "<br />isHex() - Hexa Number Reconverted: ".$y;       

    if($string==$y){
        echo "<br /> Result: Hexa ";
    }else{
        echo "<br /> Result: NOT Hexa";
    }   
    }


function IsOcta($string){
    (int) $x=octdec("$string");     // Input must be a String and octdec returns NUMBER
          $y=decoct($x);            // Must be a Number and decoct returns STRING
    echo "<br />IsOcta() - Octal Number Reconverted: ".$y;          

    if($string==$y){
    echo "<br /> Result: OCTAL";
    }else{
    echo "<br /> Result: NOT OCTAL";
    }

} 

Here is the Call to the Functions:

$hex    =   "x23";      // STRING 
$octa   =   "023";  // STRING 
echo "<br /    Original HEX = $hex | Original Octa = $octa <br /    ";

echo isHex($hex)."<br /    ";   
echo IsOcta($octa);

Here is the Result of the Function Call:

Original HEX = x23 | Original Octa = 023 

isHex() - Hexa Number Reconverted: 23
Result: NOT Hexa

IsOcta() - Octal Number Reconverted: 23
Result: OCTAL

===== COMPLETE ANSWER ====

Thanks Layke for directing to the built in function that tests whether HEXA DECIMAL characters are present in a string or not. Also thanks mario for giving the hint to use ltrim. Both functions were needed to get isHexa or is hexadecimal function to be built.

--- EDITED FUNCTION --

// isHEX function

function isHex($strings){

// Does not work as originally suggested by Layke, but thanks for directing to the resource. It does not omit 0x representation of a hexadecimal number. 
/*
foreach ($strings as $testcase) {
     if (ctype_xdigit($testcase)) {
        echo "<br /> $testcase - <strong>TRUE</strong>, Contains only Hex<br />";
    } else {
        echo "<br /> $testcase - False, Is not Hex";

   } 
}
*/

   // This works CORRECTLY
   foreach ($strings as $testcase) {
        if (ctype_xdigit(ltrim($testcase , "0x"))) {
           echo "<br /> $testcase - <strong>TRUE</strong>, Contains only Hex<br />";
       } else {
           echo "<br /> $testcase - False, Is not Hex";

      } 
   }
}

$strings = array('AB10BC99', 'AR1012', 'x23' ,'0x12345678');
isHex($strings); // calling

Probably now, is this the fool proof answer to the 'is hexadecimal' function?

Answer

Layke picture Layke · Oct 28, 2012

isHexadecimal?

PHP has built in functions for hexadecimal.

See the function here ctype_xdigit: http://uk1.php.net/ctype_xdigit

<?php
$strings = array('AB10BC99', 'AR1012', 'ab12bc99');
foreach ($strings as $testcase) {
    if (ctype_xdigit($testcase)) {
        // TRUE : Contains only Hex
    } else {
        // False : Is not Hex
    }
}

isOctal?

And for figuring out if a number is octal, you can just flip and flip.

function isOctal($x) {
    return decoct(octdec($x)) == $x;
}