Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:22:29+00:00 2026-06-13T11:22:29+00:00

I have Two Functions. IsOcta and isHex . Can not seem to make isHex

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:22:30+00:00Added an answer on June 13, 2026 at 11:22 am

    isHexadecimal?

    PHP has built in functions for hexadecimal.

    See the function here ctype_xdigit: https://www.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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions that are very similar and i'd like to make a
I have two functions here say animation1 and load1 they work like that: function
I have two functions. How can i make from this 2 jquery effects /
I have two functions that loads on document ready. They work fine when they
I have two functions that i'm hoping i can put into one and can
I have two functions that make $.getJSON calls - one looks at JSON stored
I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have two functions that caluclate and adjust the height and width of elements
I have two functions that have different enough logic but pretty much the same
I have two functions that are supposed to encrypt and decrypt a string but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.