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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • 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 strchr() – Alias of strstr()
  2. PHP function bin2hex – Convert binary data into hexadecimal representation
  3. PHP function acos – Arc cosine
  4. PHP addslashes function – Quote string with slashes
  5. PHP cos function – Cosine
  6. PHP function chop() – Alias of rtrim()
  7. PHP asin function – Arc sine
  8. PHP function stripcslashes() – Un-quote string quoted with addcslashes()
  9. PHP function strcmp() – Binary safe string comparison
  10. PHP function str_replace() – Replace all occurrences of the search string with the replacement string

You May Also Like

Primary Sidebar

Recent Posts

  • “glab repo” Command Examples
  • “glab release” Command Examples
  • “glab pipeline” Command Examples
  • “glab mr” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright