Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

hexadecimal
decimal
php
communitycreator

What is the dechex method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The dechex method can be used to convert a decimal number to a hexadecimal string.

Hexadecimal is the base-16 number system and uses the digits from 0 to 9 and A to F.

Syntax

dechex(int $num): string

Arguments

The argument is the decimal number to be converted to hexadecimal.

The dechex method only handles the unsigned integers. If we pass negative integers that will be treated as unsigned integers.

The largest decimal value that can be converted to decimal in a 32-bit platform is PHP_INT_MAX * 2 + 1 (or -1).

Return value

It returns the hexadecimal value of the argument as a string.

Code

<?php 
  $num = 15;
  echo "hexadeciaml value of ". $num. " is: ". dechex($num)."\n";

  $num = 16;
  echo "hexadeciaml value of ". $num. " is: ". dechex($num)."\n";

  $num = 100;
  echo "hexadeciaml value of ". $num. " is: ". dechex($num)."\n";

  $num = 999;
  echo "hexadeciaml value of ". $num. " is: ". dechex($num)."\n";
?>

Explanation

In the code above, we have used the dechex method to convert the decimal numbers 15, 16, 100, and 999 into hexadecimal representations.

RELATED TAGS

hexadecimal
decimal
php
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring