如何将类型转换为字节数组(byte array)在 Golang 中。

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

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 (
	&quot;bytes&quot;
	&quot;fmt&quot;
	&quot;reflect&quot;
)

type Signature [5]byte

const (
	/// Number of bytes in a signature.
	SignatureLength = 5
)

func main() {

	var bytes0to64 Signature = SignatureFromBytes([]byte(&quot;Here is a string..........&quot;))
	fmt.Println(reflect.TypeOf(bytes0to64))

	res := bytes.Compare([]byte(&quot;Test&quot;), bytes0to64)
	if res == 0 {
		fmt.Println(&quot;!..Slices are equal..!&quot;)
	} else {
		fmt.Println(&quot;!..Slice are not equal..!&quot;)
	}

}

func SignatureFromBytes(in []byte) (out Signature) {
	byteCount := len(in)
	if byteCount == 0 {
		return
	}

	max := SignatureLength
	if byteCount &lt; 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(&quot;Here is a string..........&quot;))
	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(&quot;Test&quot;), bytes0to64[:])
if res == 0 {
	fmt.Println(&quot;!..Slices are equal..!&quot;)
} else {
	fmt.Println(&quot;!..Slice are not equal..!&quot;)
}
res = bytes.Compare([]byte{72, 101, 114, 101, 32}, bytes0to64[:])
if res == 0 {
	fmt.Println(&quot;!..Slices are equal..!&quot;)
} else {
	fmt.Println(&quot;!..Slice are not equal..!&quot;)
}

This will output (try it on the Go Playground):

!..Slice are not equal..!
!..Slices are equal..!

huangapple
  • 本文由 发表于 2022年3月15日 20:05:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/71481930.html
匿名

发表评论

匿名网友

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

确定