首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bytes32值问题

bytes32值问题
EN

Ethereum用户
提问于 2018-02-16 11:19:52
回答 1查看 3.5K关注 0票数 3

我正面临着bytes32的问题。

这是我的合同样本:

代码语言:javascript
复制
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() })

我觉得这不正常..。知道我可能做错了什么吗?

谢谢

EN

回答 1

Ethereum用户

回答已采纳

发布于 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()) });

票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/39885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档