How can I convert uint64 into string in Soldity

Viewed 148
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.0;

import "./Strings.sol";

contract NFTTest {

   uint64 serialNum = 1;

   string public uri;
   string public uriPrefix ="abc/";
   string public uriSuffix = ".json";

function setURI() external {
   uri = uriPrefix + Strings.toString(serialNum) + uriSuffix;
}

But the issue is I get the error: Operator + not compatible with types string storage ref and string memory

Not sure what I'm doing wrong. I'm using the Strings.sol library.

2 Answers

Openzeppelin's String.toString() accepts a uint of uint256. So before using toString, cast your uint64 to uint256.

enter image description here

For example:

variable = String.toString( uint256( numberInUint64 ));

You would want to use abi.encodePacked to combine multiple strings

For example:

   uri = string(abi.encodePacked(uriPrefix, Strings.toString(serialNum), uriSuffix));
Related