سؤال

I am attempting to XOR two values. If I do I can get the right result, however, using string() on it results in a random byte being added to it!

Can anyone explain this?

Here's a playground: http://play.golang.org/p/tIOOjqo_Fe

هل كانت مفيدة؟

المحلول

So, you have:

z := 175 // 0xaf

That is the unicode code point for the character: ¯

The following line of code will then take the value and treat it as a unicode code point (rune) and turn it into a utf-8 encoded string:

out := string(z)

In utf-8 encoding, that character would be represented by two bytes: []byte(0xc2, 0xaf)

So, the bytes you see are the Go string's utf-8 encoding.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top