Using PHP to Convert ASCII Character to Decimal Eq

2020-02-11 06:52发布

问题:

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

回答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
)


回答2:

ord() is what you need



回答3:

ord() returns the integer ascii value of a character

chr() returns a character from an ascii value



回答4:

Try ord.



回答5:

Just to add onto streetparade's answer

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

returns raw dec for the characters.