在数组或切片字面量中使用字符串常量作为字节值。

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

Use string constant as byte value in array or slice literal

问题

我正在尝试从字符串中创建一个全局字节数组:

point := []byte{0x23f32...}

但是我遇到了错误:

untyped string constant "0x23f32...) as byte value in array or slice literal

我知道我应该使用type conversion将字符串转换为字节切片,即使用()而不是{}

point := []byte(0x23f32...) 

问题中的代码是一个复合字面量。然而,我正在使用运算符作为全局变量,所以我认为我不能以这种方式声明变量。此外,在代码的后面,从逻辑上讲,我还将使用[33]byte,所以我担心如何正确声明point []byte,以免后面出现类型错误,比如"mismatched types [32]byte and []byte"

因此,在考虑这两个问题的情况下,请告诉我如何正确处理point := []byte

英文:

I am trying to make a global byte array from a string with:

point := []byte{0x23f32...}

But I am getting the error:

untyped string constant "0x23f32...) as byte value in array or slice literal

I know what I should use a type conversion to convert a string to a slice of bytes, i.e. use of () instead of {}.

point := []byte(0x23f32...) 

The code in the question is a composite literal. However, I am using operators as a global variable, so I think that I cannot declare the variable that way. Also, further in the code, logically, I will also have to use [33]byte, so I'm worried about how to declare point []byte here so that I don't have a type error later like "mismatched types [32]byte and []byte".

So keeping these two questions in mind, could you please tell me how to deal with point := []byte here correctly?

答案1

得分: 1

0x23f32... 是一个整数字面量,不能转换为字节(或字节数组)。对于 var point = []byte{0x23f32},你将会得到以下编译时错误:

cannot use 0x23f32 (untyped int constant 147250) as byte value in array or slice literal (overflows)

假设你期望从以下代码中获得的结果(从你的问题中并不清楚):

point := []byte{0x23f32 . . . }

实际上是:

point := []byte{0x23,0xf3,0x2? . . . }

...

首先,如果你想声明一个“全局”变量(例如,一个 包范围 变量),你需要使用 var <name> <type> = <value> 的形式。不能使用 <name> := <value>

其次,你可以通过几种不同的方式初始化一个 []byte

  • var p1 []byte = []byte{ 0x12, 0x23, 0x45, . . . }
    一个包含十六进制常量的数组/切片,每个常量的范围必须在 0x00-0xFF(0-255 十进制)之间。

  • var p2 []byte = []byte{ "\x12\x23\x45 . . ." }
    一个包含所需字节的字符串。

  • var p3 []byte = []byte{ 18, 35, 69 . . . }
    一个包含十进制常量的数组/切片。

  • var p4 []byte = []byte{ '\x12', '\x23', '\x45' . . . }
    一个包含单个字节字面量的数组/切片。请注意,如果指定的值超出了字节的范围(0x00-0xFF,0-255 十进制),你将会得到类似以下的编译时错误:

    cannot use 'ш' (untyped rune constant 1096) as byte value in array or slice literal (overflows)

请注意,上述 4 个示例都产生完全相同的值。

英文:

0x23f32... is a integer literal and won't convert to a byte (or bytes). You will get this compile-time error for var point = []byte{0x23f32}

> cannot use 0x23f32 (untyped int constant 147250) as byte value in array or slice literal (overflows)

Assuming that what you expect to get (it's not clear from your question) from something like

point := []byte{0x23f32 . . . }

is essentially

point := []byte{0x23,0xf3,0x2? . . . }

...

First, if you are trying to declare a "global" variable (e.g. a package-scoped variable), you need to use the var &lt;name&gt; &lt;type&gt; = &lt;value&gt; form. You can't use &lt;name&gt; := &lt;value&gt;.

Second, you can initialize a []byte in a couple different ways:

  • var p1 []byte = []byte{ 0x12, 0x23, 0x45, . . . }<br/>
    an array/slice of hex constants, each needs to be in the range 0x00-0xFF (0-255 decimal).

  • var p2 []byte = []byte{ &quot;\x12\x23\x45 . . .&quot; }<br/>
    a string containing the desired bytes.

  • var p3 []byte = []byte{ 18, 35, 69 . . . }<br/>
    an array/slice of decimal constants

  • var p4 []byte = []byte{ &#39;\x12&#39;, &#39;\x23&#39;, &#39;\x45&#39; . . . }<br/>
    an array slice of individual byte literals. Note that if one specifies a value outside the range of a byte (0x00–0xFF, 0-255 decimal), you'll get a compile time error along the lines of:

    > cannot use &#39;ш&#39; (untyped rune constant 1096) as byte value in array or slice literal (overflows)

Note that the above 4 examples all yield exactly the same value.

答案2

得分: 0

如果你需要将32位数字转换为字节,你可以这样做:

package main

import (
   "encoding/binary"
   "fmt"
)

func main() {
   var point [4]byte
   binary.BigEndian.PutUint32(point[:], 0x23f32)
   fmt.Println(point) // [0 2 63 50]
}

https://godocs.io/encoding/binary#ByteOrder.PutUint32

英文:

If you need to convert 32 bit number to bytes, you can do this:

package main

import (
   &quot;encoding/binary&quot;
   &quot;fmt&quot;
)

func main() {
   var point [4]byte
   binary.BigEndian.PutUint32(point[:], 0x23f32)
   fmt.Println(point) // [0 2 63 50]
}

https://godocs.io/encoding/binary#ByteOrder.PutUint32

huangapple
  • 本文由 发表于 2022年10月1日 06:11:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/73914387.html
匿名

发表评论

匿名网友

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

确定