英文:
How to convert type to byte array golang
问题
如何将一种类型转换为字节数组
这是一个工作示例
package main
import (
"bytes"
"fmt"
"reflect"
)
type Signature [5]byte
const (
/// 签名中的字节数。
SignatureLength = 5
)
func main() {
var bytes0to64 Signature = SignatureFromBytes([]byte("Here is a string.........."))
fmt.Println(reflect.TypeOf(bytes0to64))
res := bytes.Compare([]byte("Test"), bytes0to64)
if res == 0 {
fmt.Println("!..Slices are equal..!")
} else {
fmt.Println("!..Slice are not equal..!")
}
}
func SignatureFromBytes(in []byte) (out Signature) {
byteCount := len(in)
if byteCount == 0 {
return
}
max := SignatureLength
if byteCount < max {
max = byteCount
}
copy(out[:], in[0:max])
return
}
在Go语言中定义了
type Signature [5]byte
所以这是预期的
var bytes0to64 Signature = SignatureFromBytes([]byte("Here is a string.........."))
fmt.Println(reflect.TypeOf(bytes0to64))
它只输出类型为
main.Signature
这是正确的,现在我想从中获取字节数组以进行下一级处理,并得到编译错误
./prog.go:23:29: cannot use bytes0to64 (type Signature) as type []byte in argument to bytes.Compare
Go build failed.
错误是正确的,只是比较时存在不匹配。现在我应该如何将Signature类型转换为字节数组?
英文:
How to Convert Type of one kind to byte array
Here is the working example
package main
import (
"bytes"
"fmt"
"reflect"
)
type Signature [5]byte
const (
/// Number of bytes in a signature.
SignatureLength = 5
)
func main() {
var bytes0to64 Signature = SignatureFromBytes([]byte("Here is a string.........."))
fmt.Println(reflect.TypeOf(bytes0to64))
res := bytes.Compare([]byte("Test"), bytes0to64)
if res == 0 {
fmt.Println("!..Slices are equal..!")
} else {
fmt.Println("!..Slice are not equal..!")
}
}
func SignatureFromBytes(in []byte) (out Signature) {
byteCount := len(in)
if byteCount == 0 {
return
}
max := SignatureLength
if byteCount < max {
max = byteCount
}
copy(out[:], in[0:max])
return
}
In Go lang defined
type Signature [5]byte
So this is expected
var bytes0to64 Signature = SignatureFromBytes([]byte("Here is a string.........."))
fmt.Println(reflect.TypeOf(bytes0to64))
It just output the Type to
main.Signature
This is correct, now I want to get the byte array from this for the next level of processing and get a compilation error
./prog.go:23:29: cannot use bytes0to64 (type Signature) as type []byte in argument to bytes.Compare
Go build failed.
The error is right only there is a mismatch on comparison. Now how should i convert the Signature type to byte array
答案1
得分: 3
由于Signature
是一个字节数组,你可以简单地对它进行切片:
bytes0to64[:]
这将得到一个[]byte
类型的值。
测试一下:
res := bytes.Compare([]byte("Test"), bytes0to64[:])
if res == 0 {
fmt.Println("!..Slices are equal..!")
} else {
fmt.Println("!..Slice are not equal..!")
}
res = bytes.Compare([]byte{72, 101, 114, 101, 32}, bytes0to64[:])
if res == 0 {
fmt.Println("!..Slices are equal..!")
} else {
fmt.Println("!..Slice are not equal..!")
}
这将输出(在Go Playground上尝试):
!..Slice are not equal..!
!..Slices are equal..!
英文:
Since Signature
is a byte array, you may simply slice it:
bytes0to64[:]
This will result in a value of []byte
.
Testing it:
res := bytes.Compare([]byte("Test"), bytes0to64[:])
if res == 0 {
fmt.Println("!..Slices are equal..!")
} else {
fmt.Println("!..Slice are not equal..!")
}
res = bytes.Compare([]byte{72, 101, 114, 101, 32}, bytes0to64[:])
if res == 0 {
fmt.Println("!..Slices are equal..!")
} else {
fmt.Println("!..Slice are not equal..!")
}
This will output (try it on the Go Playground):
!..Slice are not equal..!
!..Slices are equal..!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论