为什么将无符号整数转换为字节数组形式以“ 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?

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

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

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

如果我具有类似效果的 strong>: p> \ n

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

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

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

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.