如何将uint8数组转换为字符串
问题描述:
What's the best way to convert from []uint8 to string?
I'm using http://github.com/confluentinc/confluent-kafka-go/kafka
To read events from kafka. But it does not return plain string event. It returns event with type []uint8 How can I convert this event from []uint8 to string?
从[] uint8转换为字符串的最佳方法是什么? p>
我正在使用 http://github.com/confluentinc/confluent-kafka-go/ kafka p>
从kafka读取事件。 但是它不会返回纯字符串事件。 它将返回类型为[] uint8的事件 如何将该事件从[] uint8转换为字符串? p> div>
答
byte
is an alias for uint8
, which means that a slice of uint8
) (aka []uint8
) is also a slice of byte
(aka []byte
).
And byte slices and strings are directly convertible, due to the fact that strings are backed by byte slices:
myByteSlice := []byte{ ... } // same as myByteSlice := []uint8{ ... }
myString := string(myByteSlice) // myString is a string representation of the byte slice
myOtherSlice := []byte(myString) // Converted back to byte slice