Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert uint8 slice to string

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?

like image 449
Le D. Thang Avatar asked Jun 09 '26 23:06

Le D. Thang


1 Answers

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
like image 59
Flimzy Avatar answered Jun 12 '26 12:06

Flimzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!