For web3 0.x:
You need to use web3.fromAscii(val) to write correct bytes32 input to contract.
And you are right, anyone can read it with web3.toAscii(val).
Full web3.js code in terms of your designations:
//sets bytes32
testInstance.test.sendTransaction(web3.fromAscii("20160528"), options);
//gets bytes32
testInstance.Date.call(function(err, val) {
if (err) console.log(err);
console.log(web3.toAscii(val));
// will print "20160528"
});
For web3 1.0:
Note: fromAscii has been deprecated
You need to use web3.utils.asciiToHex(val) to write correct bytes32 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:
//sets bytes32
testInstance.methods.test(web3.utils.asciiToHex("20160528")).send(options);
//gets bytes32
testInstance.methods.Date().call(function(err, val) {
if (err) console.log(err);
console.log(web3.utils.hexToAscii(val));
// will print "20160528"
});
20160528? – Gawey Jul 26 '17 at 06:58