将字符串转换为整数值

时间:2011-01-17 13:33:19

标签: php network-programming unix mathematical-optimization

有字符串 010100

现在这是十六进制的。您将如何获得结果值:

id = 4;
part = 1;
setting = 0;
increment=0;

知道在这个字符串中id应该是10位值,部分是6位值,设置2位值,增量6位你将如何生成字符串?

感谢您的帮助。

抱歉,错过了值增量,这样更有意义......

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

//      123456789012345612123456
$str = '000001010110000111000111';
//              21    33 3     7
$id = base_convert(substr($str, 0, 10), 2, 10);
$part = base_convert(substr($str, 10, 6), 2, 10);
$setting = base_convert(substr($str, 16, 2), 2, 10);
$increment = base_convert(substr($str, 18, 6), 2, 10);
echo "id = $id\npart = $part\nsetting = $setting\nincrement = $increment\n";

<强>输出:

id = 21
part = 33
setting = 3
increment = 7