英文:
Convert string array to byte array in go
问题
给定一个只包含单个字符的字符串数组,例如:
ex := [...]string{"a","o",".",".","2",".",".","9"}
是否有一种方法可以获得一个字节数组,其内容与字符串数组相同,但是使用字节而不是字符串?
英文:
Given an string array which only contains single characters such as:
ex := [...]string{"a","o",".",".","2",".",".","9"}
is there a way to get a byte array with same content but with bytes instead of strings?
答案1
得分: 3
使用转换将每个string
转换为[]byte
。
ex := [...]string{"a", "o", ".", ".", "2", ".", ".", "9"}
var ey [len(ex)][]byte
for i := range ex {
ey[i] = []byte(ex[i])
}
如果你的目的是获取连接字符串的字节数组,可以使用这段代码。这段代码仅在字符串为单个ASCII字符时有效。
ex := [...]string{"a", "o", ".", ".", "2", ".", ".", "9"}
var ey [len(ex)]byte
for i := range ex {
ey[i] = ex[i][0]
}
如果你想要获取连接字符串的字节切片,可以使用以下表达式:[]byte(strings.Join(ex[:], ""))
。
ex := []string{"a", "o", ".", ".", "2", ".", ".", "9"}
ey := make([][]byte, len(ex))
for i := range ex {
ey[i] = []byte(ex[i])
}
..
s := []byte(strings.Join(ex, ""))
英文:
Use a conversion to convert each string
to a []byte
.
ex := [...]string{"a", "o", ".", ".", "2", ".", ".", "9"}
var ey [len(ex)][]byte
for i := range ex {
ey[i] = []byte(ex[i])
}
Use this code if your intent is to get a byte array of the joined strings. This code only works when the strings are single ASCII characters.
ex := [...]string{"a", "o", ".", ".", "2", ".", ".", "9"}
var ey [len(ex)]byte
for i := range ex {
ey[i] = ex[i][0]
}
Use this expression of you want to get a slice of bytes of the joined strings: []byte(strings.Join(ex[:], ""))
I don't know the your context for doing this, but my guess is that it's more appropriate to use a slice than an array:
ex := []string{"a", "o", ".", ".", "2", ".", ".", "9"}
ey := make([][]byte, len(ex))
for i := range ex {
ey[i] = []byte(ex[i])
}
..
s := []byte(strings.Join(ex, ""))
答案2
得分: 0
这似乎可以实现:
package main
import (
"fmt"
"strings"
)
func main() {
a := [...]string{"a","o",".",".","2",".",".","9"}
var b [len(a)]byte
c := strings.Join(a[:], "")
copy(b[:], c)
// [8]uint8{0x61, 0x6f, 0x2e, 0x2e, 0x32, 0x2e, 0x2e, 0x39}
fmt.Printf("%#v\n", b)
}
https://godocs.io/strings#Join
英文:
This seems to do it:
package main
import (
"fmt"
"strings"
)
func main() {
a := [...]string{"a","o",".",".","2",".",".","9"}
var b [len(a)]byte
c := strings.Join(a[:], "")
copy(b[:], c)
// [8]uint8{0x61, 0x6f, 0x2e, 0x2e, 0x32, 0x2e, 0x2e, 0x39}
fmt.Printf("%#v\n", b)
}
答案3
得分: 0
根据这是否是代码生成流程的一部分,你可以有几种方法来实现。
直接方式:
bs := [...]byte{'a', 'o', '.', '.', '2', '.', '.', '9'}
或者间接方式:
ex := [...]string{"a", "o", ".", ".", "2", ".", ".", "9"}
bs := [...]byte{
ex[0][0],
ex[1][0],
ex[2][0],
ex[3][0],
ex[4][0],
ex[5][0],
ex[6][0],
ex[7][0],
} // 类型为 [8]int8,即 [8]byte
根据你的使用情况,这些方法可能过于死板。对于动态初始化方法,请参考这里的其他答案。
https://play.golang.org/p/iMEjFpCKAaW
英文:
Depending on if this is part of a code-generation pipeline, you can do this a couple of ways.
Directly:
bs := [...]byte{'a', 'o', '.', '.', '2', '.', '.', '9'}
or indirectly:
ex := [...]string{"a", "o", ".", ".", "2", ".", ".", "9"}
bs := [...]byte{
ex[0][0],
ex[1][0],
ex[2][0],
ex[3][0],
ex[4][0],
ex[5][0],
ex[6][0],
ex[7][0],
} // type [8]int8 i.e. [8]byte
https://play.golang.org/p/iMEjFpCKAaW
Depending on your use case, these ways may be too rigid. For dynamic initialization methods see the other answers here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论