I have a contract with a public variable that's bytes32. If I enter the value 20160528 then it auto converts to bytes32. Is there anyway to convert it back from the nodejs terminal? I've been trying
web3.toAscii(testInstance.Date.call())
but it just gives me nonsense. Here is the the test code:
contract test {
bytes32 public Date;
function test(bytes32 _date){
Date=_date;
}
}
Best Answer
For web3 0.x:
You need to use
web3.fromAscii(val)
to write correctbytes32
input to contract.And you are right, anyone can read it with
web3.toAscii(val)
.Full web3.js code in terms of your designations:
For web3 1.0:
Note:
fromAscii
has been deprecatedYou need to use
web3.utils.asciiToHex(val)
to write correctbytes32
input to contract.And you are right, anyone can read it with
web3.utils.hexToAscii(val)
.Full web3.js code in terms of your designations: