Skip to content

Commit

Permalink
Fix toHex for string number
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jan 19, 2024
1 parent 7aba28a commit 6c3a335
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/Formatters/HexFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ class HexFormatter implements IFormatter
*/
public static function format($value)
{
$value = Utils::toString($value);
$value = mb_strtolower($value);

if (Utils::isZeroPrefixed($value)) {
if (is_string($value) && Utils::isZeroPrefixed($value)) {
$value = mb_strtolower($value);
return $value;
} else {
$value = Utils::toHex($value, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Utils
*/
public static function toHex($value, $isPrefix=false)
{
if (is_numeric($value)) {
if (is_int($value) || is_float($value)) {
// turn to hex number
$bn = self::toBn($value);
$hex = $bn->toHex(true);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/HexFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testFormat()
$this->assertEquals($hex, '0xabce');

$hex = $formatter->format('123');
$this->assertEquals($hex, '0x7b');
$this->assertEquals($hex, '0x313233');

$hex = $formatter->format(12);
$this->assertEquals($hex, '0xc');
Expand Down
10 changes: 5 additions & 5 deletions test/unit/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,24 @@ public function testToHex()
$this->assertEquals('0x' . $this->testHex, Utils::toHex('hello world', true));

$this->assertEquals('0x927c0', Utils::toHex(0x0927c0, true));
$this->assertEquals('0x927c0', Utils::toHex('600000', true));
$this->assertEquals('0x363030303030', 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('0x3630303030', Utils::toHex('60000', true));
$this->assertEquals('0xea60', Utils::toHex(60000, true));
$this->assertEquals('0xea60', Utils::toHex(new BigNumber(60000), true));

$this->assertEquals('0x', Utils::toHex(0x00, true));
$this->assertEquals('0x', Utils::toHex('0', true));
$this->assertEquals('0x30', Utils::toHex('0', true));
$this->assertEquals('0x', Utils::toHex(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('0x3438', Utils::toHex('48', true));
$this->assertEquals('30', Utils::toHex(48));
$this->assertEquals('30', Utils::toHex('48'));
$this->assertEquals('3438', Utils::toHex('48'));

$this->assertEquals('0x30', Utils::toHex(new BigNumber(48), true));
$this->assertEquals('0x30', Utils::toHex(new BigNumber('48'), true));
Expand Down

0 comments on commit 6c3a335

Please sign in to comment.