144

[]byte to string raises an error. string([]byte[:n]) raises an error too. By the way, for example, sha1 value to string for filename. Does it need utf-8 or any other encoding set explicitly? Thanks!

I159
  • 29,741
  • 31
  • 97
  • 132
lindsay show
  • 1,571
  • 2
  • 9
  • 6
  • 8
    `[]byte` can be converted directly to `string`. Please show an example of what problem you're having. – JimB Nov 16 '16 at 13:59
  • eg. file hash value to string for filename – lindsay show Nov 17 '16 at 02:21
  • 1
    If you just show us an example of what you tried, it would be a very easy question to answer, rather than waiting for us to guess the correct answer. – JimB Nov 17 '16 at 16:40
  • In most modern languages it would be perfectly clear what he was trying to do, sadly, and without any additional context needed. I also stumbled upon this question looking for the same answer (as answered below). – milosmns Nov 03 '19 at 17:46
  • @JimB Byte arrays cannot be directly converted to a string. Only byte slices. – Rawley Fowler Mar 06 '22 at 21:20

7 Answers7

179

The easiest method I use to convert byte to string is:

myString := string(myBytes[:])
Pang
  • 9,564
  • 146
  • 81
  • 122
warbear0129
  • 1,799
  • 2
  • 8
  • 2
  • 19
    Why the `:`? What's the difference between this answer and `myString := string(myBytes)`? – Ferran Maylinch Dec 14 '21 at 09:19
  • 12
    The `:` is needed because byte *arrays* cannot be directly turned to a string while slices can. Let's say we have `var b [64]byte` . `string(b)` will fail while `string(b[:])` will work. – MKrup Jan 04 '22 at 16:26
104

The easiest way to convert []byte to string in Go:

myString := string(myBytes)

Note: to convert a "sha1 value to string" like you're asking, it needs to be encoded first, since a hash is binary. The traditional encoding for SHA hashes is hex (import "encoding/hex"):

myString := hex.EncodeToString(sha1bytes)
rustyx
  • 80,671
  • 25
  • 200
  • 267
16

In Go you convert a byte array (utf-8) to a string by doing string(bytes) so in your example, it should be string(byte[:n]) assuming byte is a slice of bytes.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Franck Jeannin
  • 6,107
  • 3
  • 21
  • 33
  • I have just try like this.but failed. In fact ,I need to convert a file sha1 value to string ,named for the filename – lindsay show Nov 17 '16 at 02:11
15

I am not sure that i understand question correctly, but may be:

var ab20 [20]byte = sha1.Sum([]byte("filename.txt"))
var sx16 string = fmt.Sprintf("%x", ab20)
fmt.Print(sx16)

https://play.golang.org/p/haChjjsH0-

alarcl
  • 342
  • 3
  • 8
  • 1
    While technically correct, it's rather unusual in Go to use `var` and declare every type without just inferring them. https://play.golang.org/p/JUl57LKfzk – JimB Nov 17 '16 at 16:39
  • 1
    Ah! "%x"! That's what I was doing wrong! :) I was using the wrong Sprintf placeholder, duh... – Gwyneth Llewelyn Jun 04 '17 at 17:52
  • You could also use `hex.EncodeToString` instead of `fmt.Sprintf("%x", ...)`. – Olshansky Jan 06 '22 at 23:48
4
ToBe := [6]byte{65, 66, 67, 226, 130, 172}
s:=ToBe[:3]
// this will work
fmt.Printf("%s",string(s))
// this will not
fmt.Printf("%s",string(ToBe))

Difference : ToBe is an array whereas s is a slice.

Dev Kalra
  • 41
  • 1
1

First you're getting all these negatives reviews because you didn't provided any code. Second, without a good example. This is what i'd do

var Buf bytes.Buffer
Buf.Write([]byte)
myString := Buf.String()
Buf.Reset() // Reset the buffer to reuse later

or better yet

myString := string(someByteArray[:n])

see here also see @JimB's comment

That being said if you help that targets your program, please provide and example of what you've tried, the expect results, and error.

Community
  • 1
  • 1
reticentroot
  • 3,612
  • 2
  • 22
  • 39
0

We can just guess what is wrong with your code because no meaningful example is provided. But first what I see that string([]byte[:n]) is not valid at all. []byte[:n] is not a valid expression because no memory allocated for the array. Since byte array could be converted to string directly I assume that you have just a syntax error.

Shortest valid is fmt.Println(string([]byte{'g', 'o'}))

I159
  • 29,741
  • 31
  • 97
  • 132