/** * @return BufferInterface */ private function serialize() { if ($this->math->cmp($this->number, '0') === 0) { return new Buffer('', 0); } // Using array of integers instead of bytes $result = []; $negative = $this->math->cmp($this->number, 0) < 0; $abs = $negative ? $this->math->sub(0, $this->number) : $this->number; while ($this->math->cmp($abs, 0) > 0) { //array_unshift($result, (int)$this->math->bitwiseAnd($abs, 0xff)); $result[] = (int) $this->math->bitwiseAnd($abs, 0xff); $abs = $this->math->rightShift($abs, 8); } if ($result[count($result) - 1] & 0x80) { //array_unshift($result, $negative ? 0x80 : 0); $result[] = $negative ? 0x80 : 0; } else { if ($negative) { //$result[0] |= 0x80; $result[count($result) - 1] |= 0x80; } } $s = ''; foreach ($result as $i) { $s .= chr($i); } return new Buffer($s, null, $this->math); }
/** * @return Buffer */ private function serialize() { if ($this->math->cmp($this->number, '0') === 0) { return new Buffer('', 4); } // Using array of integers instead of bytes $result = []; $negative = $this->math->cmp($this->number, 0) < 0; $abs = $negative ? $this->math->sub(0, $this->number) : $this->number; while ($this->math->cmp($abs, 0) > 0) { array_unshift($result, (int) $this->math->bitwiseAnd($abs, 0xff)); $abs = $this->math->rightShift($abs, 8); } if ($result[0] & 0x80) { array_unshift($result, $negative ? 0x80 : 0x0); } else { if ($negative) { array_unshift($result, 0x80); } } return new Buffer(array_reduce($result, function ($agg, $current) { return $agg . chr($current); }), 4, $this->math); }