Byte array to string without conversion

Hi, i’m trying to get a byte array to a string but preserving the contents? (Sort of hard to explain)

So, I have something like this:

myhash := sha256.sha256(“random-string-to-hash”)
fmt.Printf(“%x\n”, myhash)

and I get as output something like this: e0f1d812e21465d6d37cfd5212b5733168f635ef6720e0c0f1e14bc09efcc0fe

But I would like this as a string and not a byte. I can’t use string(myhash) as this would convert each byte in the array into char and i would get garbage.

Does anyone know a way?

hashStr := fmt.Sprintf("%x", myhash)

(or hex.EncodeToString if you don’t already have a fmt dependency)

1 Like

Perfect, thanks! Do you know of a similar way to go the other way around?

https://golang.org/pkg/encoding/hex/#DecodeString is probably what you want.

1 Like

Thank you calmh!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.