Thursday, January 16, 2025
HomeConversionsHow to convert byte array to string in Go?

How to convert byte array to string in Go? [duplicate]

To convert a byte array to a string in Go, use the built-in string() function. Here’s an example:

package main

import “fmt”

func main() {
byteArray := []byte{72, 101, 108, 108, 111} // ASCII for “Hello”
str := string(byteArray)
fmt.Println(str) // Output: Hello
}

See also  How many cups are in 10 ounces?

The string() function efficiently converts the byte slice to a string. This is useful when dealing with encoded data like UTF-8 or ASCII. Note that improper encoding or non-textual data may result in unexpected output. For custom encoding, use packages like encoding/json or encoding/base64.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x