From ed57c92446a51a35ba0fd790409e1d25c3790efb Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 25 Jan 2018 11:49:21 +0800 Subject: [PATCH] Change utils::toHex. --- src/Utils.php | 22 +++++++++++++++++----- test/unit/UtilsTest.php | 22 ++++++++++++++++------ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/Utils.php b/src/Utils.php index 3c48fa9..9bc06c3 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -74,20 +74,32 @@ class Utils /** * toHex + * Encoding string or integer or numeric string(is not zero prefixed) or bug number to hex. * - * @param string $value + * @param string|int|BigNumber $value * @param bool $isPrefix * @return string */ public static function toHex($value, $isPrefix=false) { - if (!is_string($value)) { - throw new InvalidArgumentException('The value to toHex function must be string.'); + if (is_numeric($value)) { + // turn to hex number + $bn = self::toBn($value); + $hex = $bn->toHex(true); + $hex = preg_replace('/^0+(?!$)/', '', $hex); + } elseif ($value instanceof BigNumber) { + $hex = $value->toHex(true); + $hex = preg_replace('/^0+(?!$)/', '', $hex); + } elseif (is_string($value)) { + $value = self::stripZero($value); + $hex = implode('', unpack('H*', $value)); + } else { + throw new InvalidArgumentException('The value to toHex function is not support.'); } if ($isPrefix) { - return '0x' . implode('', unpack('H*', $value)); + return '0x' . $hex; } - return implode('', unpack('H*', $value)); + return $hex; } /** diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index d496318..3fb7d8b 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -4,7 +4,7 @@ namespace Test\Unit; use InvalidArgumentException; use Test\TestCase; -use phpseclib\Math\BigInteger; +use phpseclib\Math\BigInteger as BigNumber; use Web3\Utils; class UtilsTest extends TestCase @@ -68,13 +68,23 @@ class UtilsTest extends TestCase */ public function testToHex() { - $hex = Utils::toHex('hello world'); + $this->assertEquals($this->testHex, Utils::toHex('hello world')); + $this->assertEquals('0x' . $this->testHex, Utils::toHex('hello world', true)); - $this->assertEquals($hex, $this->testHex); + $this->assertEquals('0x927c0', Utils::toHex(0x0927c0, true)); + $this->assertEquals('0x927c0', Utils::toHex('600000', true)); + $this->assertEquals('0x927c0', Utils::toHex(600000, true)); + $this->assertEquals('0x927c0', Utils::toHex(new BigNumber(600000), true)); + + $this->assertEquals('0xea60', Utils::toHex(0x0ea60, true)); + $this->assertEquals('0xea60', Utils::toHex('60000', true)); + $this->assertEquals('0xea60', Utils::toHex(60000, true)); + $this->assertEquals('0xea60', Utils::toHex(new BigNumber(60000), true)); - $hexPrefixed = Utils::toHex('hello world', true); - - $this->assertEquals($hexPrefixed, '0x' . $this->testHex); + $this->assertEquals('0x', Utils::toHex(0x00, true)); + $this->assertEquals('0x', Utils::toHex('0', true)); + $this->assertEquals('0x', Utils::toHex(0, true)); + $this->assertEquals('0x', Utils::toHex(new BigNumber(0), true)); } /**