0

I have a string, byteArray:

byteArray := []byte("Hello, 世界-123..")
fmt.Println(byteArray)

which looks like:

[72 101 108 108 111 44 32 228 184 150 231 149 140 45 49 50 51 46 46]

I need to get byteArray[0] as a string, like "72", but they're byte type.

How do I achieve this?

2
  • 1
    You are looking for strconv.Itoa or strconv.FormatUint. That's not hex encoding, though. Commented Apr 24, 2019 at 6:27
  • Thank, strconv.Itoa will work.I found an answer here, link Commented Apr 24, 2019 at 6:42

2 Answers 2

1

You can use strconv.Itoa:

byteArray := []byte("Hello, 世界-123..")

for _, v := range byteArray {
    s := strconv.Itoa(int(v))
    fmt.Printf("%T, %v\n", s, s)
}
Sign up to request clarification or add additional context in comments.

Comments

-2

There you go.

byteArray := []byte("Hello, 世界-123..")
fmt.Println(string(byteArray[0]))

// H

1 Comment

``` string(byteArray[0] ``` this will turn byte "72" to string "H"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.