为什么Go会向我的字符串添加字节?

huangapple go评论161阅读模式
英文:

Why is Go adding bytes to my string?

问题

当我在字符串中添加一个大于或等于0x80的单个字节时,golang会在我的字节之前添加0xc2。我认为这与utf8 runes有关。无论如何,我该如何只在字符串末尾添加0x80呢?

示例:

var s string = ""
len(s) // 这将为0
s += string(0x80)
len(s) // 这将为2,字符串现在是字节0xc2 0x80
英文:

When I add a single byte to my string at 0x80 or above, golang will add 0xc2 before my byte.
I think this has something to do with utf8 runes. Either way, how do I just add 0x80 to the end of my string?

Example:

var s string = ""
len(s) // this will be 0
s += string(0x80)
len(s) // this will be 2, string is now bytes 0xc2 0x80

答案1

得分: 2

规范中可以得知:

将有符号或无符号整数值转换为字符串类型会产生一个包含整数的UTF-8表示的字符串。

表达式string(0x80)的结果是一个包含0x80的UTF-8表示的字符串,而不是一个包含单个字节0x80的字符串。0x80的UTF-8表示是0xc2 0x80。

要在字符串中指定字节0x80,可以使用\x十六进制转义:

 s += "\x80"

你可以使用string([]byte)转换来从任意字节序列创建字符串:

 s += string([]byte{0x80})
英文:

The From the specification:

> Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.

The expression string(0x80) evaluates to a string with the UTF-8 representation of 0x80, not a string containing the single byte 0x80. The UTF-8 representation of 0x80 is 0xc2 0x80.

Use the \x hex escape to specify the byte 0x80 in a string:

 s += "\x80"

You can create a string from an arbitrary sequence of bytes using the string([]byte) conversion.

 s += string([]byte{0x80})

答案2

得分: 0

我还没有找到避免添加那个字符的方法,如果我使用string(0x80)来转换字节。然而,我发现如果我将整个字符串转换为字节切片,然后添加字节,再转换回字符串,我可以在字符串中得到正确的字节顺序。

示例:

bytearray := []byte(some_string)
bytearray = append(bytearray, 0x80)
some_string = string(bytearray)

这种方法有点愚蠢,如果有人找到更好的方法,请发表。

英文:

I haven't found a way to avoid adding that character, if I use string(0x80) to convert the byte. However, I did find that if I change the whole string to a slice of bytes, then add the byte, then switch back to a string, I can get the correct byte order in the string.

Example:

bytearray := []byte(some_string)
bytearray = append(bytearray, 0x80)
some_string = string(bytearray)

Kind of a silly work around, if anyone finds a better method, please post it.

huangapple
  • 本文由 发表于 2015年9月20日 04:41:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/32673014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定