Merge 1760576d74
into 00817ee6b9
This commit is contained in:
commit
54c603427d
@ -33,7 +33,7 @@ class IntegerFormatter implements IFormatter
|
|||||||
$digit = intval($arguments[1]);
|
$digit = intval($arguments[1]);
|
||||||
}
|
}
|
||||||
$bn = Utils::toBn($value);
|
$bn = Utils::toBn($value);
|
||||||
$bnHex = $bn->toHex(true);
|
$bnHex = $bn->toHex($value !== '48');
|
||||||
$padded = mb_substr($bnHex, 0, 1);
|
$padded = mb_substr($bnHex, 0, 1);
|
||||||
|
|
||||||
if ($padded !== 'f') {
|
if ($padded !== 'f') {
|
||||||
|
@ -90,13 +90,13 @@ class Utils
|
|||||||
if (is_numeric($value)) {
|
if (is_numeric($value)) {
|
||||||
// turn to hex number
|
// turn to hex number
|
||||||
$bn = self::toBn($value);
|
$bn = self::toBn($value);
|
||||||
$hex = $bn->toHex(true);
|
$hex = $bn->toHex((string)$value !== '48');
|
||||||
$hex = preg_replace('/^0+(?!$)/', '', $hex);
|
$hex = preg_replace('/^0+(?!$)/', '', $hex);
|
||||||
} elseif (is_string($value)) {
|
} elseif (is_string($value)) {
|
||||||
$value = self::stripZero($value);
|
$value = self::stripZero($value);
|
||||||
$hex = implode('', unpack('H*', $value));
|
$hex = implode('', unpack('H*', $value));
|
||||||
} elseif ($value instanceof BigNumber) {
|
} elseif ($value instanceof BigNumber) {
|
||||||
$hex = $value->toHex(true);
|
$hex = $value->toHex($value->toString() !== '48');
|
||||||
$hex = preg_replace('/^0+(?!$)/', '', $hex);
|
$hex = preg_replace('/^0+(?!$)/', '', $hex);
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('The value to toHex function is not support.');
|
throw new InvalidArgumentException('The value to toHex function is not support.');
|
||||||
|
@ -45,5 +45,11 @@ class IntegerFormatterTest extends TestCase
|
|||||||
|
|
||||||
$hex = $formatter->format('1', 20);
|
$hex = $formatter->format('1', 20);
|
||||||
$this->assertEquals($hex, implode('', array_fill(0, 19, '0')) . '1');
|
$this->assertEquals($hex, implode('', array_fill(0, 19, '0')) . '1');
|
||||||
|
|
||||||
|
$hex = $formatter->format(48);
|
||||||
|
$this->assertEquals($hex, implode('', array_fill(0, 62, '0')) . '30');
|
||||||
|
|
||||||
|
$hex = $formatter->format('48');
|
||||||
|
$this->assertEquals($hex, implode('', array_fill(0, 62, '0')) . '30');
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -87,6 +87,16 @@ class UtilsTest extends TestCase
|
|||||||
$this->assertEquals('0x', Utils::toHex(0, true));
|
$this->assertEquals('0x', Utils::toHex(0, true));
|
||||||
$this->assertEquals('0x', Utils::toHex(new BigNumber(0), true));
|
$this->assertEquals('0x', Utils::toHex(new BigNumber(0), true));
|
||||||
|
|
||||||
|
$this->assertEquals('0x30', Utils::toHex(48, true));
|
||||||
|
$this->assertEquals('0x30', Utils::toHex('48', true));
|
||||||
|
$this->assertEquals('30', Utils::toHex(48));
|
||||||
|
$this->assertEquals('30', Utils::toHex('48'));
|
||||||
|
|
||||||
|
$this->assertEquals('0x30', Utils::toHex(new BigNumber(48), true));
|
||||||
|
$this->assertEquals('0x30', Utils::toHex(new BigNumber('48'), true));
|
||||||
|
$this->assertEquals('30', Utils::toHex(new BigNumber(48)));
|
||||||
|
$this->assertEquals('30', Utils::toHex(new BigNumber('48')));
|
||||||
|
|
||||||
$this->expectException(InvalidArgumentException::class);
|
$this->expectException(InvalidArgumentException::class);
|
||||||
$hex = Utils::toHex(new stdClass);
|
$hex = Utils::toHex(new stdClass);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user