phpsigned-integer

Convert normal integer to 8 bit integer in PHP


I have an Integer 138 that i want to covert into an 8 bit signed Integer that will become -118. I have tried unpack, intval and several othe php function but they didn't worked. How should i do this, stuck in this for few days now.


Solution

  • There may be other ways, but a quick look at using pack and unpack - pack with C stores it as an unsigned and the unpack with c gets the signed value back...

    echo unpack( 'c', pack('C', 138))[1];
    

    gives

    -118