• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

PHP base_convert function – Convert a number between arbitrary bases

by admin

Description

Converts a number from one base to another. The base the number is currently in is from, and the base to convert to is to. The bases to convert from and to must be between 2 and 36. Digits in a base higher than 10 are represented with the letters a (10) through z (35). Up to a 32-bit number, or 2,147,483,647 decimal, can be converted.

Syntax:

base_convert(string $num, int $from_base, int $to_base): string

It is impractical for PHP to include separate functions to convert every base to every other base, so they are grouped into one function: base_convert(). This takes three parameters: a number to convert, the base to convert from, and the base to convert to. For example, the following two lines are identical:

print decbin(16);
print base_convert("16", 10, 2);

The latter is just a more verbose way of saying “convert the number 16 from base 10 to base 2.” The advantage of using base_convert() is that we can now convert binary directly to hexadecimal, or even crazier combinations, such as octal to duodecimal (base 12) or hexadecimal to vigesimal (base 20).

Highest Base Supported

The highest base that base_convert() supports is base 36, which uses 0-9 and then A-Z. If you try to use a base larger than 36, you will get an error.

Parameters

Parameter Description
num The number to convert. Any invalid characters in num are silently ignored.
from_base The base num is in
to_base The base to convert num to

Return Values

num converted to base to_base.

Examples

Example 1: Convert a number’s base:

$org = "564"; 
$new = base_convert($org, 8, 16); 
echo "$org in octal is the same as $new in hexadecimal\n"; 

Example 2:

$hexadecimal = 'a37334';
echo base_convert($hexadecimal, 16, 2);

Output:

101000110111001100110100

Filed Under: PHP

Some more articles you might also be interested in …

  1. PHP function strcmp() – Binary safe string comparison
  2. PHP asin function – Arc sine
  3. PHP function chunk_split() – Split a string into smaller chunks
  4. PHP function str_replace() – Replace all occurrences of the search string with the replacement string
  5. htaccess Cheatsheet
  6. PHP addslashes function – Quote string with slashes
  7. PHP function abs – Absolute value
  8. PHP function strcasecmp() – Binary safe case-insensitive string comparison
  9. PHP function str_ireplace() – Case-insensitive version of str_replace()
  10. PHP Function str_word_count() – Return information about words used in a string

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright