我想把字符串赋值给bytes数组:
var arr [20]byte
str := "abc"
for k, v := range []byte(str) {
arr[k] = byte(v)
}
还有别的方法吗?
我想把字符串赋值给bytes数组:
var arr [20]byte
str := "abc"
for k, v := range []byte(str) {
arr[k] = byte(v)
}
还有别的方法吗?
当前回答
string -> []byte用于将字符串转换为字节切片:
[]byte(str)
将数组转换为切片,使用[20]byte -> []byte:
arr[:]
将字符串复制到数组,使用string -> [20]byte:
copy(arr[:], str)
与上面相同,但首先显式地将字符串转换为slice:
copy(arr[:], []byte(str))
The built-in copy function only copies to a slice, from a slice. Arrays are "the underlying data", while slices are "a viewport into underlying data". Using [:] makes an array qualify as a slice. A string does not qualify as a slice that can be copied to, but it qualifies as a slice that can be copied from (strings are immutable). If the string is too long, copy will only copy the part of the string that fits (and multi-byte runes may then be copied only partly, which will corrupt the last rune of the resulting string).
这段代码:
var arr [20]byte
copy(arr[:], "abc")
fmt.Printf("array: %v (%T)\n", arr, arr)
...给出如下输出:
array: [97 98 99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] ([20]uint8)
我还在围棋游乐场提供了它
其他回答
除了上面提到的方法,你还可以做一个小技巧
s := "hello"
b := *(*[]byte)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s))))
Go Play: http://play.golang.org/p/xASsiSpQmC
你不应该使用这个:-)
string -> []byte用于将字符串转换为字节切片:
[]byte(str)
将数组转换为切片,使用[20]byte -> []byte:
arr[:]
将字符串复制到数组,使用string -> [20]byte:
copy(arr[:], str)
与上面相同,但首先显式地将字符串转换为slice:
copy(arr[:], []byte(str))
The built-in copy function only copies to a slice, from a slice. Arrays are "the underlying data", while slices are "a viewport into underlying data". Using [:] makes an array qualify as a slice. A string does not qualify as a slice that can be copied to, but it qualifies as a slice that can be copied from (strings are immutable). If the string is too long, copy will only copy the part of the string that fits (and multi-byte runes may then be copied only partly, which will corrupt the last rune of the resulting string).
这段代码:
var arr [20]byte
copy(arr[:], "abc")
fmt.Printf("array: %v (%T)\n", arr, arr)
...给出如下输出:
array: [97 98 99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] ([20]uint8)
我还在围棋游乐场提供了它
将字符串转换为字节片
您需要一种快速的方法将[]字符串转换为[]字节类型。用于将文本数据存储到随机访问文件或要求输入数据为[]字节类型的其他类型的数据操作等情况。
package main
func main() {
var s string
//...
b := []byte(s)
//...
}
这在使用ioutil时很有用。WriteFile,它接受字节片作为它的数据参数:
WriteFile func(filename string, data []byte, perm os.FileMode) error
另一个例子
package main
import (
"fmt"
"strings"
)
func main() {
stringSlice := []string{"hello", "world"}
stringByte := strings.Join(stringSlice, " ")
// Byte array value
fmt.Println([]byte(stringByte))
// Corresponding string value
fmt.Println(string([]byte(stringByte)))
}
输出:
你好,世界
请检查链接操场
最终创建了特定于数组的方法来实现这一点。很像每个int类型都有特定方法的编码/二进制包。例如binary.BigEndian。uint16 PutUint16([]字节)。
func byte16PutString(s string) [16]byte {
var a [16]byte
if len(s) > 16 {
copy(a[:], s)
} else {
copy(a[16-len(s):], s)
}
return a
}
var b [16]byte
b = byte16PutString("abc")
fmt.Printf("%v\n", b)
输出:
[0 0 0 0 0 0 0 0 0 0 0 0 0 97 98 99]
请注意,我希望填充在左边,而不是右边。
http://play.golang.org/p/7tNumnJaiN
小菜一碟:
arr := []byte("That's all folks!!")