How to use base_convert() Function In PHP

The base_convert() function converts a given number to the 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.

Syntax:

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

Example:

PHP




<?php
  
function hexToDec($hex) {
    return base_convert($hex, 16, 10);
}
  
$hexNum = '634E24';
$decimalNum = hexToDec($hexNum);
  
echo "Decimal Value: " . $decimalNum;
  
?>


Output

Decimal Value: 6508068

Hexadecimal to Decimal Conversion in PHP

Given a Hexadecimal Number, and the task is to convert Hexadecimal to a Decimal Number using PHP.

Examples:

Input: hexNum = '1A'
Output: 

Input: hexNum = '634E24'
Output: 6508068

There are three different methods to convert Hexadecimal to Decimal number, these are:

 

Table of Content

  • Using hexdec() Function
  • Using base_convert() Function
  • Using for Loop

Similar Reads

Using hexdec() Function

The hexdec() function converts a hexadecimal number to a decimal number. This function converts numbers that are too large to fit into the integer type, larger values are returned as a float in that case. If hexdec() encounters any non-hexadecimal characters, it ignores them....

Using base_convert() Function

...

Using for Loop

The base_convert() function converts a given number to the 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....