hex - php decode or parsing each nibble of hexadecimal -
hex - php decode or parsing each nibble of hexadecimal -
how can decode hex value in php?
i have hex value encodes data.
for ex: hex value = 0x 1121 0031 here, each nibble of hex value tells me first nibble 1 means product_1 , 2 product_2. , sec nibble 1 means new product, 2 means old product.
how can parse each nibble?
you can extract each nibble straight string , compare this:
$data = '0x 1121 0031'; $data = substr($data, 2); //remove 0x prefix string $data = str_replace(' ', '', $data); //remove spaces string //$data '11210031' echo 'the product number ' . $data[0] . "\n"; if ($data[1] == 1) { echo "this new product\n"; } else if ($data[1] == 2) { echo "this used product\n"; }
you can interpret string number , extract bits:
$data = '0x 1121 0031'; $data = substr($data, 2); //remove 0x prefix string $data = str_replace(' ', '', $data); //remove spaces string //$data '11210031' $number = hexdec($data); //convert hexadecimal number integer //$number 0x11210031 (hexadecimal) = 287375409 (decimal) $nibble1 = ($number >> 28) & 0xf; //shift number right 28 bits (each nibble 4 bits) , select lastly 4 bits (0xf selects bits in lastly nibble) echo "the product number $nibble1\n"; $nibble2 = ($number >> 24) & 0xf; if ($nibble2 == 1) { echo "this new product\n"; } else if ($nibble2 == 2) { echo "this used product\n"; }
php hex decode php4 php-5.5
Comments
Post a Comment