views:

75

answers:

4

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

   } 
A: 
$serial = 00032432; //here you have an octal number
$serial= strval($serial); //now you have a string
if ($serial[0]=="0") {
 //do something
}
naivists
This is not working. I added an echo to the if statement and it is not showing. The strval and other functions like (string) are not working either with these octals? Thanks for assisting!
krio
Ok, another question, how do you get this variable to be a number? Do you explicitly cast it to (int) after you receive it from the database? Then do not do that, since it is actually not a number, but a string!
naivists
A: 

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

adam
I had tried those earlier. The octals are still being shown as their equivalents and not as strings. ie - echo octdec(decoct(045)); shows as 37 when I would like for it to show as 045 in a string.
krio
A: 

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;

Coronatus
I tried this as well. For example run this: $a = (string) 012;echo $a;.... it echos 10 not 012.
krio
@krio that makes sense, because the octal value is converted *before* the string conversion. It should not be the case in your example, though.
Pekka
Now that I read it - I am getting the variable from a database, however it is assigned to a value before being used. It is not directly being inserted into the query.
krio
+1  A: 

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.

Nicolò Martini
How can I know if it is octal before running the base_convert? For example, I don't want to run base_convert on a non-octal because it will change the value. Is there a way to check BEFORE running this function and it alters the values? I only want to run it on the values with leading zeros.
krio
I don't understand what you mean. You get the numbers in a string format, i think, from the db. Then you have a string, and you can check the zeros... But I think I haven't understood which is the problem, so if you give me more details about the problem, with a more detailed example...
Nicolò Martini
I get the strings from the DB and assign them to variables. When I try to run string functions on them they are not working on numbers with leading zeros (ie 00532), however on numbers without leading zeros (ie 532) it works fine. When I use base_convert only for numbers with leading zeros it works correctly, but then numbers without leading zeros are incorrectly converted. The problem is I don't know which numbers to use the base_convert function on because I can't tell if they have leading zeros without converting them first. Does that make sense? I had never even heard of octals before =/
krio
But if you have already octals in string, why can't you work directly on them? And check for the leading zeros in that string? However, can you post an example of the two different cases with the base_convert function, saying what the result should be?
Nicolò Martini
Ok, after running through a debugger I found that there was a small line in the app that was taking these strings and performing an operation on them (which was causing the octal conversion). I chose this as the answer because the recommended base_convert function helped immensely with solving the original problem of comparing the strings after they are converted to an octal. Thanks!
krio