我正面临着bytes32的问题。
这是我的合同样本:
pragma solidity ^0.4.16;
contract Test {
bytes32 public input;
function test(bytes32 _in) public {
input = _in;
}
}问题是在松露开发中,无论我如何运行测试函数,输入值都是相同的。例如,如果我运行:
MyContract.deployed().then(inst => { inst.test(12,{from:>web3.eth.accounts}) };
然后我得到了结果:
松露(开发)> MyContract.deployed().then(inst => {返回>inst.input.call() });
另外,如果我跑:
MyContract.deployed().then(inst => { inst.test("12",{from: web3.eth.accounts}) });
结果是相同的:
'0xc000000000000000000000000000000000000000000000000000000000000000‘() MyContract.deployed().then(inst => {返回inst.input.call() })
我觉得这不正常..。知道我可能做错了什么吗?
谢谢
发布于 2018-02-16 11:41:30
字节表示为十六进制值。在您的例子中,base10的0xc是12。
在web3js中,基于ABI方法将值转换为相应的数据类型,并将数据传输到网络中。在javascript中,有时大整数/十六进制值表示为字符串。在发送到实际网络之前,web3js将转换为eq值。
这意味着字符串的"12“被转换为整数并转换为十六进制值。
字符串类型的" 12“,但它的数值类型(因为它只包含0-9值),因此web3js将转换为整数类型,转换为12.12十六进制表示形式为0xc。但你提到了它的bytes32。它将垫32位。所以最后的十六进制字符串应该是0xc000000000000000000000000000000000000000000000000000000000000000
使用web3.utils.fromAscii(string)将转换为字符串字节(十六进制字符串)
尝试更改以下的字符串行
MyContract.deployed().then(inst => {inst.test(web3.fromasi(“123”),{from:web3.eth.accounts}) };
您的输出将是0x3132000000000000000000000000000000000000000000000000000000000000 bytes32
作为字符串获取值:
MyContract.deployed().then(inst => {web3.toAsii(inst.input()) });
https://ethereum.stackexchange.com/questions/39885
复制相似问题