How to convert byte array to string in Go

huangapple go评论78阅读模式
英文:

How to convert byte array to string in Go

问题

[]byte转换为字符串会引发错误。
string([]byte[:n])也会引发错误。
顺便问一下,例如将sha1值转换为文件名的字符串。
是否需要显式设置utf-8或其他编码?
谢谢!

英文:

[]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!

答案1

得分: 176

我用来将 byte 转换为 string 的最简单方法是:

myString := string(myBytes[:])
英文:

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

myString := string(myBytes[:])

答案2

得分: 102

在Go语言中,将[]byte转换为string的最简单方法是:

myString := string(myBytes)

注意:如果你要将一个"sha1 value to string"转换,首先需要对其进行编码,因为哈希值是二进制的。SHA哈希值的传统编码方式是十六进制(import "encoding/hex"):

myString := hex.EncodeToString(sha1bytes)
英文:

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)

答案3

得分: 16

在Go语言中,你可以通过string(bytes)将字节数组(utf-8编码)转换为字符串。所以在你的例子中,应该是string(byte[:n]),假设byte是一个字节切片。

英文:

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.

答案4

得分: 15

我不确定我是否正确理解了问题,但可能是这样的:

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-

英文:

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-

答案5

得分: 4

ToBe := [6]byte{65, 66, 67, 226, 130, 172}
s := ToBe[:3]
// 这样可以工作
fmt.Printf("%s", string(s))
// 这样不行
fmt.Printf("%s", string(ToBe))

区别:ToBe 是一个数组,而 s 是一个切片。

<details>
<summary>英文:</summary>

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.



</details>



# 答案6
**得分**: 1

首先,你之所以得到这些负面评价,是因为你没有提供任何代码。
其次,没有一个好的示例。这是我会做的:

```go
var Buf bytes.Buffer
Buf.Write([]byte)
myString := Buf.String()
Buf.Reset() // 重置缓冲区以便以后重用

或者更好的是:

myString := string(someByteArray[:n])

在这里也可以查看这里,还可以看看@JimB的评论。

话虽如此,如果你需要针对你的程序的帮助,请提供你尝试过的示例、期望的结果和错误信息。

英文:

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.

答案7

得分: 0

我们只能猜测你的代码有什么问题,因为没有提供有意义的示例。但首先我看到string([]byte[:n])根本不是有效的。[]byte[:n]不是一个有效的表达式,因为没有为数组分配内存。由于字节数组可以直接转换为字符串,我假设你只是有一个语法错误。

最简短的有效写法是fmt.Println(string([]byte{'g', 'o'}))

英文:

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{&#39;g&#39;, &#39;o&#39;}))

huangapple
  • 本文由 发表于 2016年11月16日 20:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/40632802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定