Jim Fell
Jim Fell

Reputation: 14256

Using PHP to Convert ASCII Character to Decimal Equivalent

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

Upvotes: 5

Views: 18412

Answers (5)

php_coder_3809625
php_coder_3809625

Reputation: 193

Just to add onto streetparade's answer

foreach ($array as $decval)
{
  echo $decval;
}

returns raw dec for the characters.

Upvotes: 0

jkndrkn
jkndrkn

Reputation: 4062

ord() is what you need

Upvotes: 5

streetparade
streetparade

Reputation: 32888

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
)

Upvotes: 12

dnagirl
dnagirl

Reputation: 20456

ord() returns the integer ascii value of a character

chr() returns a character from an ascii value

Upvotes: 3

fbrereto
fbrereto

Reputation: 35925

Try ord.

Upvotes: 1

Related Questions