Using PHP to Convert ASCII Character to Decimal Equivalent

16,703

Solution 1

function ascii_to_dec($str)
{
  for ($i = 0, $j = strlen($str); $i < $j; $i++) {
    $dec_array[] = ord($str{$i});
  }
  return $dec_array;
}

example usage :

$ascii ="\t";
print_r( ascii_to_dec($ascii));

returns an array

Array
(
    [0] => 9
)

Solution 2

ord() is what you need

Solution 3

ord() returns the integer ascii value of a character

chr() returns a character from an ascii value

Solution 4

Try ord.

Share:
16,703
Jim Fell
Author by

Jim Fell

Technical expertise resides in developing firmware and PC-interface software for embedded systems using C/C++/C# programming languages, integrated development environments, and low-level debugging techniques. I enjoy developing embedded and PC drivers for wired communications. Previous work focused on the development of firmware for USB, CAN, SPI, RS-232, RS-485, ZigBee, and analog drivers, as well as implementing low-power embedded solutions across a broad range of embedded and desktop platforms.

Updated on June 05, 2022

Comments

  • Jim Fell
    Jim Fell almost 2 years

    Can someone suggest a (preferably) graceful way to convert an ASCII character to its decimal equivalent using PHP?