使用 PHP 八进制和字符串转换

发布于 2024-08-26 07:31:25 字数 399 浏览 11 评论 0原文

我正在使用一个数据库,该数据库包含一堆以前导 0 为前缀的序列号。

因此,序列号可能看起来像 00032432 或 56332432。

PHP 的问题是我不明白八进制转换系统是如何工作的。

一个具体的例子是,我试图将所有这些基于整数的数字与字符串进行转换和比较。

是否可以将八进制(例如 00234)转换为“00234”这样的字符串,以便我可以比较它?

编辑 - 添加一个具体示例。我希望能够在串行上运行 str 函数,如下所示。

  $serial = 00032432; // coming from DB

  if(substr($serial, 0, 1) == '0') {

        // do something

   } 

I'm working with a database that has a bunch of serial numbers that are prefixed with leading 0's.

So a serial number can look like 00032432 or 56332432.

Problem is with PHP I don't understand how the conversion system with octals works.

A specific example is that I'm trying to convert and compare all of these integer based numbers with strings.

Is it possible to convert an octal, such as 00234 to a string like "00234" so that I can compare it?

edit - adding a specific example. I would like to be able to run str functions on the serial like below.

  $serial = 00032432; // coming from DB

  if(substr($serial, 0, 1) == '0') {

        // do something

   } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

林空鹿饮溪 2024-09-02 07:31:25

当你用 (string) $number 转换时,你总是得到一个十进制的字符串,无论你以八进制模式还是十进制模式写数字都没关系,int就是int,它本身没有基础。这是他的字符串表示形式,必须用基数来解释。

您可以通过以下方式获取数字的八进制字符串表示形式:

$str = base_convert((string) 00032432, 10, 8);

或以十进制表示形式给出数字:

$str = base_convert((string) 13594, 10, 8);    

或者,更简洁但不太清楚:

 $str = base_convert(00032432, 10, 8);
 $str = base_convert(13594, 10, 8);

最后,隐式进行字符串转换。这些示例给出了所有结果 $str = "32432"。

base_convert 将数字的字符串表示形式从基数转换为另一个基数

如果您还想要字符串中的零,则可以通过简单的数学将它们相加。

希望这可以帮助你。

When you convert with (string) $number, you always get a string in decimal base, it doesn't matter if you write the number in octal mode or in decimal mode, an int is an int and it has not itself a base. It's his string representation that have to be interpreted with a base.

You can get the octal string representation of a number in this way:

$str = base_convert((string) 00032432, 10, 8);

or giving the number in decimal rep:

$str = base_convert((string) 13594, 10, 8);    

or, more concisely but less clearly:

 $str = base_convert(00032432, 10, 8);
 $str = base_convert(13594, 10, 8);

In the last the string conversion is made implicitly. The examples give all as result $str = "32432".

base_convert converts a string representation of a number from a base to another

If you want also the zeros in your string, you can add them with simple math.

Hope this can help you.

原谅我要高飞 2024-09-02 07:31:25
$str = sprintf('%o', $octal_value);
$str = sprintf('%o', $octal_value);
彻夜缠绵 2024-09-02 07:31:25

要将八进制转换为字符串,请对其进行强制转换:

$str = (string) 00032432;

您可以使用函数 octdec 和 decoct 在八进制和十进制之间进行转换

<?php
echo octdec('77') . "\n";
echo octdec(decoct(45));
?>

http://uk.php.net/manual/en/function.octdec.php

To convert an octal to a string, cast it:

$str = (string) 00032432;

You can convert between octal and decimal with the functions octdec and decoct

<?php
echo octdec('77') . "\n";
echo octdec(decoct(45));
?>

http://uk.php.net/manual/en/function.octdec.php

2024-09-02 07:31:25
$serial = 00032432; //here you have an octal number
$serial= strval($serial); //now you have a string
if ($serial[0]=="0") {
 //do something
}
$serial = 00032432; //here you have an octal number
$serial= strval($serial); //now you have a string
if ($serial[0]=="0") {
 //do something
}
撧情箌佬 2024-09-02 07:31:25

数据库中的所有内容都会自动成为字符串。整数是字符串,日期是字符串,狗是字符串。

如果您正在做一些奇怪的事情,请通过以下方式将任何内容转换为字符串: $a = (string) 12;

Everything from the database is automatically a string. Integers are strings, dates are strings, dogs are strings.

If you are doing something weird, convert anything to a string by: $a = (string) 12;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文