assign hexadecimal value to a variable

Discussion in 'PHP' started by rajoo.sharma, Dec 13, 2007.

  1. #1
    assigning a constant by prefixing 0x to a variable stores it as hex value, not as a string.
    $a=0xa1; //this 161 in decimal

    but if I already have a value somewhere, then how do I assign it same as above?
    $some_value = "a10ce2";
    $a= substr($some_value, 0, 2); //this is not same as $a=0xa1;

    I've to use it for XOR checksum, I already have values in a text file, so I'll read a line and use XOR to validate.

    $a=0x87;$b=0xa0;$c=0x03;$d=0x00;$e=0x0c;$f=0x28;
    echo $a ^ $b ^ $c ^ $d ^ $e ^ $f; //perfectly returns 0;

    but this is a constant assigned to a variable, when we already have values:

    $values = "87a003000c28";

    $a=substr($values, 0, 2);
    $b=substr($values, 2, 2);
    $c=substr($values, 4, 2);
    $d=substr($values, 6, 2);
    $e=substr($values, 8, 2);
    $f=substr($values, 10, 2);
    echo $a^$b^$c^$d^$e^$f; //does not return 0;


    :confused:
     
    rajoo.sharma, Dec 13, 2007 IP
  2. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    
    $values = "87a003000c28";
    $stra=substr($values, 0, 2);
    $strb=substr($values, 2, 2);
    ...
    eval ("\$a = 0x$stra;\$b = 0x$strb;");
    echo $a^$b^$c^$d^$e^$f;
    
    PHP:
    ... and so on. Does that work?
     
    theOtherOne, Dec 13, 2007 IP
  3. rajoo.sharma

    rajoo.sharma Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    :D Hey great, this worked, thanks a ton, buddy you are great.

    Thanks again
    Regards
    Rajeev
     
    rajoo.sharma, Dec 13, 2007 IP