$ch是一个角色。
我需要把它转换成像这样的2个数字(示例):
$ch = 'A' => ASCII code: 0x41
=> Binary: 0100 0001
=> {4, 1}实现这一目标的最简单、最快的方法是什么?
发布于 2010-06-06 23:06:06
可以使用ord()函数获取ASCII值,然后使用decbin和dechex将其转换为二进制和十六进制格式
http://www.php.net/manual/en/function.ord.php http://www.php.net/manual/en/function.decbin.php http://www.php.net/manual/en/function.dechex.php
发布于 2010-06-06 23:06:01
$i = ord($chr);
$hex = dechex($i);
$ret = array($hex[0], $hex[1]);发布于 2010-06-06 23:23:28
<?
$ch = 'A';
$hex = base_convert(ord($ch), 10, 16);
$binary = base_convert(ord($ch), 10, 2);
$hex_nibbles = str_split($hex);
$hex_formatted = "0x$hex";
$binary_formatted = str_pad($binary, ceil(strlen($binary)/8)*8, '0', STR_PAD_LEFT);
$binary_formatted = preg_replace('/.{4}/', "$0 ", $binary_formatted);
$hex_nibbles_formatted = '{' . join($hex_nibbles, ', ') . '}';
echo <<<EOF
ASCII code: $hex_formatted
Binary: $binary_formatted
Hex Nibbles: $hex_nibbles_formatted
EOF;
?>https://stackoverflow.com/questions/2984654
复制相似问题