php 判断是否是16进制,php解码或解析每个十六进制的半字节

您可以提取直接从字符串中的每个蚕食它这样比较:

$data = '0x 1121 0031';

$data = substr($data, 2); //remove the 0x prefix from the string

$data = str_replace(' ', '', $data); //remove the spaces from the string

//$data is now '11210031'

echo 'the product number is ' . $data[0] . "\n";

if ($data[1] == 1) {

echo "this is a new product\n";

} else if ($data[1] == 2) {

echo "this is a used product\n";

}

您也可以解释字符串作为一个数字,然后提取位:

$data = '0x 1121 0031';

$data = substr($data, 2); //remove the 0x prefix from the string

$data = str_replace(' ', '', $data); //remove the spaces from the string

//$data is now '11210031'

$number = hexdec($data); //convert the hexadecimal number to an integer

//$number is now 0x11210031 (hexadecimal) = 287375409 (decimal)

$nibble1 = ($number >> 28) & 0xF; //shift the number right by 28 bits (each nibble is 4 bits) and select only the last 4 bits (0xF selects all bits in the last nibble)

echo "the product number is $nibble1\n";

$nibble2 = ($number >> 24) & 0xF;

if ($nibble2 == 1) {

echo "this is a new product\n";

} else if ($nibble2 == 2) {

echo "this is a used product\n";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值