八进制数 - 我可能缺少什么来检查收到的输入数字是否为八进制格式?
Octal Numbers - what I might be missing to check if the input number recieved is of an Octal Format or Not?
刚刚学习了将十进制数转换为八进制数的基础知识。现在反过来说,我似乎知道任何最后一位数字结尾是 8 或 9 的数字都不可能是八进制数。
但是,我还需要检查或执行其他任何操作来查看输入数字是否为八进制数字(除了检查最后一位数字中的 8 或 9 之外)吗? - [基本上是询问我是否遗漏了某个过程]
下面是我在 PHP 中的代码:
<?php
$iOctal = 1423;
echo "What is the Decimal Value of the octal number $iOctal?"."<br/>";
$rg_Decimal = str_split($iOctal);
//print_r($rg_Decimal);
if (end($rg_Decimal) == 8 || end($rg_Decimal) == 9){
echo "<b>Error:- </b>Unable to process your request as the input number format is not of the Octal Number Format. Please try again...";
}
if ($iOctal < 8 && $iOctal >= 0){
echo "The Decimal Value of the octal number $iOctal is $iOctal.";
}
else{
$iE = count($rg_Decimal);
--$iE;
$iDecimal = 0;
for ($iA = 0; $iA < sizeof($rg_Decimal); ++$iA){
$iDecimal += $rg_Decimal[$iA] * bcpow(8,$iE);
--$iE;
}
echo "The Decimal Value of the octal number $iOctal is <b>$iDecimal</b>";
}
?>
刚好在测试的时候,用到了网上的资源。当我给出一个特定的数字时,它说数字格式不是八进制的。但是这个数字没有以 8 或 9 结尾。
期待您的支持。
您可以使用 php 中的内置函数 octdect($number)
。
来自 http://php.net/manual/en/function.octdec.php 的例子,同样的问题:
<?php
function is_octal($x) {
return decoct(octdec($x)) == $x;
}
echo is_octal(077); // true
echo is_octal(195); // false
?>
刚刚学习了将十进制数转换为八进制数的基础知识。现在反过来说,我似乎知道任何最后一位数字结尾是 8 或 9 的数字都不可能是八进制数。 但是,我还需要检查或执行其他任何操作来查看输入数字是否为八进制数字(除了检查最后一位数字中的 8 或 9 之外)吗? - [基本上是询问我是否遗漏了某个过程]
下面是我在 PHP 中的代码:
<?php
$iOctal = 1423;
echo "What is the Decimal Value of the octal number $iOctal?"."<br/>";
$rg_Decimal = str_split($iOctal);
//print_r($rg_Decimal);
if (end($rg_Decimal) == 8 || end($rg_Decimal) == 9){
echo "<b>Error:- </b>Unable to process your request as the input number format is not of the Octal Number Format. Please try again...";
}
if ($iOctal < 8 && $iOctal >= 0){
echo "The Decimal Value of the octal number $iOctal is $iOctal.";
}
else{
$iE = count($rg_Decimal);
--$iE;
$iDecimal = 0;
for ($iA = 0; $iA < sizeof($rg_Decimal); ++$iA){
$iDecimal += $rg_Decimal[$iA] * bcpow(8,$iE);
--$iE;
}
echo "The Decimal Value of the octal number $iOctal is <b>$iDecimal</b>";
}
?>
刚好在测试的时候,用到了网上的资源。当我给出一个特定的数字时,它说数字格式不是八进制的。但是这个数字没有以 8 或 9 结尾。 期待您的支持。
您可以使用 php 中的内置函数 octdect($number)
。
来自 http://php.net/manual/en/function.octdec.php 的例子,同样的问题:
<?php
function is_octal($x) {
return decoct(octdec($x)) == $x;
}
echo is_octal(077); // true
echo is_octal(195); // false
?>