I have this type of list of contact numbers, [0825623456,0838956234], that I am storing inside solidity in uint256[] array. But when I get the list of that number in console or web application , I can only see the numbers like [825623456,838956234]. 0 has been removes as it is uint data type.

So, which data type can be used to store and fetch this type of numbers except string or bytes ?


It sounds like you need to be able to store the values in a way that "0123" and "123" have different representations.

There are many different possibilities, such as storing two numbers: the value itself (123) and the number of leading zeroes (0 or 1), but the most straightforward solution is to use strings or bytes.

If your IDs are never longer than 32 characters, I would recommend using an array of bytes32s.

On a terminology note, you're not storing numbers, so don't use a numeric type. (When it comes to numbers, 0123 and 123 are the exact same value.)

  • Yes, I have used byte32 array and stored value of number in that. So what is the scenario I am facing in that is if I am store number as "M0111" and getting it on web then it is giving me hexa string like"0x4d30313131000000000000000000000000000000000000000000000000000000" which is proper and we can convert it to "M0111" using web3.toAscii function. But if I am storing only the number withour any character in bytes32 array like "0111" then in web app the value is "0x6f00000000000000000000000000000000000000000000000000000000000000".I am not able to convert it to "0111" with toAscii function
    – Riya Soni
    Jul 31 '18 at 15:35
  • Make sure you're sending a string when you sure the value.
    – user19510
    Jul 31 '18 at 18:48
  • Yes I am sending it as string while migrating the contract like this:deployer.deploy(AssetContract, ['0825623456','0838956234','0844582103'],web3.eth.accounts, {gas: 6700000}). But the hex value that I am getting on UI or console is different for only numbers
    – Riya Soni
    Aug 1 '18 at 3:30
  • I think you'll have to share a full repro so others can try to see what you're seeing.
    – user19510
    Aug 1 '18 at 3:39
  • Yes, I have done the another post for that problem to this link : ethereum.stackexchange.com/questions/55582/…
    – Riya Soni
    Aug 1 '18 at 4:24

This answer does not take into account that the length of contact number may vary

This is web3's 'fault'. Solidity stores uints with leading zeros, but web3 cuts them off.

It's just a representation, though. You as a developer know there should be leading zeros there. You could have the presentation layer of your application add the leading zeros for your users.

How you do that, depends on the language you're writing your front-end in. If you're using JavaScript, try this solution.

--- In reaction to your comment below: ---

Instead of



 app.getContactNoList().map(n => n.valueOf().padStart(10, '0'))
  • Yes, I am using web3, but also if I am trying from truffle console, then again it is removing the 0. You can as below : truffle(development)> app.getContactNoList() [ BigNumber { s: 1, e: 8, c: [ 825623456 ] }, BigNumber { s: 1, e: 8, c: [ 838956234 ] } ]
    – Riya Soni
    Jul 31 '18 at 9:53
  • "Solidity stores uints with leading zeros"????? That should be credited as the worst answer of the day!!! And generally speaking, there is no such thing as "leading zeros" for integer types, only for string types. Jul 31 '18 at 9:59
  • @goodvibration. A uint256 uses 256 bits of storage. What is in the leading bits of a uint256 that is smaller than the max? Yes: zeros
    – Henk
    Jul 31 '18 at 10:01
  • That's got nothing to do with leading zeros (and as I mentioned, the term "leading zeros" refers to strings, not to integers). Jul 31 '18 at 10:02
  • Could you provide a source to support your claim? Because, to me, you're not making sense. As for my claim: read this
    – Henk
    Jul 31 '18 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.