普通网友
2018-12-01 12:53
浏览 105
已采纳

为什么将无符号整数转换为字节数组形式以“ 3”开头的字符串?

If I do the following in golang:

data := []byte{}
data = append(data, '1')
data = append(data, '2')

fmt.Printf("%d
", len(data))
fmt.Printf("%x
", fmt.Sprintf("%d", len(data)))

I get 2 and 32, respectively, instead of just 2 on both lines (obviously the array has only two elements).

If I do something similar-ish in solidity:

bytes memory encodedPack = abi.encodePacked(prefix, length, signedMessage)

The encodePacked function also prepends the length with 3 in the final byte array.

According to the ASCII table, 3 represents "end of text". Is this what this 3 is for?

图片转代码服务由CSDN问答提供 功能建议

如果我在golang中执行以下操作:

  data:= [] byte {} 
data = append(data,'1')
data =  append(data,'2')
 
fmt.Printf(“%d 
”,len(data))
fmt.Printf(“%x 
”,fmt.Sprintf(“%d”,len( 数据)))
   
 
 

我分别得到2和32,而不是两行都是2(显然数组只有两个元素)。

如果我具有类似效果的 \ n

 个字节的内存已编码Pack = abi.encodePacked(前缀,长度,signedMessage)
   
 
 

encodePacked 函数也位于 最后一个字节数组中带有3的长度。

根据ASCII表,3表示“文本结尾”。 这是3的目的吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongyashun2559 2018-12-01 13:06
    最佳回答

    Sprintf() returns a string. You are printing the string "2" which in ASCII is 0x32.

    The 3 here is not end-of-text. If you look at the ASCII table, you will notice that the characters "0" to "9" are encoded as 0x30 until 0x39.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题