Change utils::toHex.
This commit is contained in:
parent
4df52132d0
commit
ed57c92446
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user