user2058039
user2058039

Reputation: 3

hex to int 64 signed on php

how convert hex A67BFE427CBDC58F to int 64 signed -6450282479763995249?

function bchexdec($hex) {
 if(strlen($hex) == 1) {
     return hexdec($hex);
 } else {
     $remain = substr($hex, 0, -1);
     $last = substr($hex, -1);
     $x = bcadd(bcmul(16, bchexdec($remain)), hexdec($last));
     return $x;
 }

bchexdec('A67BFE427CBDC58F') == 11996461593945556367. This correct, but without sign(( how convert unsigned to signed?

PHP 5.3+; PHP_INT_SIZE == 4

Upvotes: 0

Views: 211

Answers (1)

nneonneo
nneonneo

Reputation: 179442

if(bccomp($x, bcpow(2, 63)) >= 0) {
    $x = bcsub($x, bcpow(2, 64);
}

Upvotes: 1

Related Questions