8

私には2つの機能があります。IsOctaおよびisHex。isHexを正しく動作させることができないようです。
isHex()の問題は、元の文字列x23の「x」表記を省略できないことです。

元の16進srtingもD1CEにすることができます。したがって、xを追加してから比較しても効果はありません。

isHex関数の正しい解決策はありますか?また、isOctaは正しいですか?

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";
    }

} 

関数の呼び出しは次のとおりです。

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

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

関数呼び出しの結果は次のとおりです。

Original HEX = x23 | Original Octa = 023 

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

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

=====完全な回答====

HEXADECIMAL文字が文字列に存在するかどうかをテストする組み込み関数を紹介してくれたLaykeに感謝します。また、ltrimを使用するためのヒントを与えてくれたマリオにも感謝します。isHexaを取得するには両方の関数が必要でしたが、ビルドする16進関数です。

---編集された機能-

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

おそらく今、これは「16進数」関数に対するばかげた答えですか?

4

3 に答える 3

21

isHexadecimal?

PHPには、16進数用の関数が組み込まれています。

ここで関数を参照してください:http ctype_xdigit//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?

また、数値が8進数であるかどうかを判断するには、フリップしてフリップするだけです。

function isOctal($x) {
    return decoct(octdec($x)) == $x;
}
于 2012-10-28T20:58:04.067 に答える
2

次に、入力文字列をクリーンアップできますltrim()。最初の変換を行う前に追加するだけです。

 $string = ltrim($string, "0x");

先行ゼロ(不要)とx文字の両方を削除します。

于 2012-10-28T20:58:14.913 に答える
1

isHexadecimal :: =

ctype_xdigit($testString)

isOctal :: =

preg_match('/^[0-7]+$/', $testString);
于 2017-11-27T16:50:20.520 に答える