View Full Version : Wondering how to do Hex Math in PHP
Trent Violence
Jan 23rd 2009, 4:26 pm
Hey guys, for a page on my site, I'm needing to do some hex math operations, I know I could probably convert the hex to a decimal and do the math that way, but I'm wondering if there's an easier, more direct way. Any help is greatly appreciated.
Kaizoku
Jan 23rd 2009, 7:40 pm
$hex = 0x1A + 0x2A; // 68
joebert
Jan 24th 2009, 3:32 am
Assuming your hexedecimal inputs will be variable, and tend to be strings when you get them, the only option I see is using hexdec or base_convert and working from there, like you were thinking in the first place.
bird.23
Jan 24th 2009, 7:27 am
<?php
$hex1 = FF;
$hex2 = AB;
$dec1 = hexdec($hex1);
$dec2 = hexdec($hex2);
$sum = $dec1 +$dec2;
echo "Sum is :$sum <br />";
?>
Danltn
Jan 24th 2009, 10:45 am
<?php
$hex1 = FF;
$hex2 = AB;
$dec1 = hexdec($hex1);
$dec2 = hexdec($hex2);
$sum = $dec1 +$dec2;
echo "Sum is :$sum <br />";
?>
How unnecessary... Not to mention FF is a horrible way of using hex...
<?php
$hex1 = 0x1F;
$hex2 = 0x10;
echo $hex1 + $hex2;
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.