downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

pi> <mt_srand
[edit] Last updated: Wed, 22 May 2013

view this page in

octdec

(PHP 4, PHP 5)

octdecConversion d'octal en décimal

Description

number octdec ( string $octal_string )

Retourne une chaîne contenant la représentation décimale du nombre octal_tring.

Liste de paramètres

octal_string

La chaîne de caractères octale à convertir

Valeurs de retour

La représentation décimale de octal_string

Historique

Version Description
4.1.0 Cette fonction peut également convertir de très grands nombres. Elle retourne un nombre de type float dans ce cas.

Exemples

Exemple #1 Exemple avec octdec()

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

L'exemple ci-dessus va afficher :

63
45

Voir aussi

  • decoct() - Convertit de décimal en octal
  • bindec() - Convertit de binaire en décimal
  • hexdec() - Convertit de hexadécimal en décimal
  • base_convert() - Convertit un nombre entre des bases arbitraires



pi> <mt_srand
[edit] Last updated: Wed, 22 May 2013
 
add a note add a note User Contributed Notes octdec - [5 notes]
up
1
contato at andersonfraga dot net
4 years ago
Number is octal?

Simple and easy:

<?php

function is_octal($x) {
    return
decoct(octdec($x)) == $x;
}

echo
is_octal(077); // true
echo is_octal(195); // false

?>

Thanks
[]'s
up
1
Anonymous
10 years ago
The 'S' flag for Unix file access rights is badly computed in the above sample.
If the corresponding 'x' bit (exec) is not set, and the 's' bit (setgid/setuid/sticky) is set, then the flag should not be displayed as and uppercase 'S', but as a lower case 's'. Also the sticky bit (mainly used for folders with public right access rights such as /tmp to protect against deletion by non owner) is badly named ("text"?).
up
0
harry at disgruntledgoat dot com
6 years ago
Calling the sticky bit "text" is not erroneous: On UNIX back in 1974, it instructed the operating system to retain the text segment of the program in swap space after the process exited. This speeded subsequent executions by allowing the kernel to make a single operation of moving the program from swap to real memory.
up
-1
meheler at searchbc dot com
11 years ago
This function and decoct work great when working with file permissions. For example:

<?php
    $mode
= octdec('4750');
   
   
$user['suid']     = ($mode & 04000);
   
$user['read']     = ($mode & 00400);
   
$user['write']    = ($mode & 00200);
   
$user['execute']  = ($mode & 00100);
   
   
$group['sgid']    = ($mode & 02000);
   
$group['read']    = ($mode & 00040);
   
$group['write']   = ($mode & 00020);
   
$group['execute'] = ($mode & 00010);
   
   
$other['text']    = ($mode & 01000);
   
$other['read']    = ($mode & 00004);
   
$other['write']   = ($mode & 00002);
   
$other['execute'] = ($mode & 00001);
   
   
$attrs = array('-','-','-','-','-','-','-','-','-','-');
   
    if (
$system['dir']) $attrs[0] = 'd';
   
    if (
$user['read']) $attrs[1] = 'r';
    if (
$user['write']) $attrs[2] = 'w';
    if (
$user['execute']) $attrs[3] = 'x';
    if (
$user['suid']) $attrs[3] = 'S';
   
    if (
$group['read']) $attrs[4] = 'r';
    if (
$group['write']) $attrs[5] = 'w';
    if (
$group['execute']) $attrs[6] = 'x';
    if (
$group['sgid']) $attrs[6] = 'S';

    if (
$other['read']) $attrs[7] = 'r';
    if (
$other['write']) $attrs[8] = 'w';
    if (
$other['execute']) $attrs[9] = 'x';
    if (
$other['text']) $attrs[9] = 'T';
   
    echo
'<pre>';
    echo
'Mode Decimal: ' . $mode . "\n";
    echo
'Mode Octal: ' . decoct($mode) . "\n";
    echo
'Mode String: ' . implode('',$attrs)."\n";
    echo
'</pre>';
?>

Outputs:

Mode Decimal: 2536
Mode Octal: 4750
Mode String: -rwSr-x---

-Mike
up
-2
cubbo at quakenet dot php
4 years ago
Hi, this is an attempt to explain octdec().

There is one bug, if input is not octal, it doesn't return a proper decimal.

<?php

$octal
= (int) "764";

function
octal_to_decimal($int)
    {               
       
$data = integer;
       
$i = (strlen($int) - 1);   
       
$parse_int = str_split($int);
        foreach(
$parse_int as $value)
            {
               
$data += (int)($value * pow(8, $i));
               
$i--;
            }       
        return
$data;
    }
/**
*    explanation of the PHP function octdec()
*/
print octal_to_decimal($octal) . "<br />";

/**
*    the PHP function octdec()
*/
print octdec($octal);

?>

 
show source | credits | sitemap | contact | advertising | mirror sites