Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP base_convert( ) Math Function
Next article icon

PHP base_convert( ) Math Function

Last Updated : 22 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The base_convert() function in PHP is used to convert a number given in an arbitrary base to a desired base. 
Both the base should be between 2 and 32 and bases with digits greater than 10 are represented with letters a-z i.e 10 is represented as a, 11 is represented as b and 35 is represented as z. 
The case of the letters is not sensitive.

Syntax:  

string base_convert($inpNumber, $fromBase, $desBase)    

Parameters Used: This function accepts three parameters and are described below: 

  1. $inpNumber : It is the number to be converted.
  2. $fromBase : It is the original base of the number.
  3. $desBase : It is the base to which you want to convert.


Return Value: It returns a string that represents the number converted to the desired base.

Examples: 

Input : base_convert(B296, 16, 8)  Output : 131226    Input : base_convert(B296, 16, 2)  Output : 1011001010010110    Input : base_convert(621, 8, 16)  Output : 191    Input : base_convert(110011, 2, 16)  Output : 33    


Below programs illustrate the base_convert() function in PHP: 
 

  • Converting hexadecimal to octal:
PHP
<?php  $hexadec = "B296";  echo base_convert($hexadec, 16, 8);  ?>       

Output: 

131226    
  • Converting hexadecimal to binary:
PHP
<?php  $hexadec = "B296";  echo base_convert($hexadec, 16, 2);  ?>       

Output: 

1011001010010110      
  • Converting octal to hexadecimal:
PHP
<?php  $octal = "621";  echo base_convert($octal, 8, 16);  ?>    

Output: 

191      
  • Converting binary to hexadecimal:
PHP
<?php  $binary = "110011";  echo base_convert($binary, 2, 16);  ?>        

Output: 

33    


Reference: 
http://php.net/manual/en/function.base-convert.php
 


Next Article
PHP base_convert( ) Math Function

S

Shubrodeep Banerjee
Improve
Article Tags :
  • Misc
  • Web Technologies
  • PHP
  • PHP-math
  • PHP-function
Practice Tags :
  • Misc

Similar Reads

    PHP mb_convert_kana() Function
    The mb_convert_kana() is an inbuilt function in PHP that is used to convert text into full-width and half-width. Syntax:mb_convert_kana($string, $mode, $encoding) : stringParameters: This function accepts three parameters that are described below. $string: This is the string that we want to convert
    2 min read
    PHP Math Functions
    PHP is a scripting language that comes with many built-in math functions and constants. These tools help you do basic math, solve more complex problems, and work with different math concepts. This guide gives an overview of PHP's math functions and constants, along with examples, practical uses, and
    5 min read
    PHP mb_convert_variables() Function
    The mb_convert_variables() is an inbuilt function in PHP that transforms the character code into variables. Syntax: mb_convert_variables( $to_encoding, $from_encoding, $var, ...$vars ): string|falseParameters: This function accepts four parameters that are described below: $to_encoding: The characte
    1 min read
    PHP decoct( ) Function
    In the earlier days of computing, octal numbers and the octal numbering system was very popular for counting inputs and outputs because as it works in counts of eight, inputs and outputs were in counts of eight, a byte at a time. Due to a wide usage of octal number system many times it happens that
    2 min read
    PHP | bcsqrt() Function
    The bcsqrt() function in PHP is an inbuilt function and is used to evaluate the square root of an arbitrary precision number. This function accepts an arbitrary precision number as a string and returns the square root of the number after scaling the result to a specified precision. Syntax: string bc
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences