在Go语言中将字节数组存储到字符串变量中。

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

storing byte array into a string variable in Go lang

问题

我第一次使用Go语言。
我有一个字节数组,我想通过套接字发送它。
目前,我的套接字数据有字符串变量msg1,msg2,msg3。
我想将我的字节数组附加到它上面。以下是代码片段。

var arr1 [4]byte = [4]byte{11,22,33,44}

addr := msg1 + msg2 + msg3
socket.Send(addr, 0)

但是当我尝试这样做时,我会得到一个错误。

addr := msg1 + msg2 + msg3 + string(arr1)

错误:无法将arr1(类型[4]byte)转换为类型string

在这种情况下我该怎么办?

实际代码

package main
import "fmt"

var arr1 [4]byte = [4]byte{11,22,33,44}
func main() {
    data := "msg1" + string(arr1[:])
  fmt.Printf("\n%s",data)
}
英文:

I am using Go Lang for the first time.
I have a byte array which I want to send over a socket.
Currently, my socket data has string variables msg1,msg2, msg3.
I want to append my byte array to it. Below is the code snippet.

var arr1 [4]byte = [4]byte{11,22,33,44}

addr := msg1 + msg2 + msg3
socket.Send(addr, 0)

But when I try to do that I get an error.

addr := msg1 + msg2 + msg3 + string(arr1)

Error: cannot convert arr1 (type [4]byte) to type string

What should I do in this case?

Actual code

package main
import "fmt"

var arr1 [4]byte = [4]byte{11,22,33,44}
func main() {
    data := "msg1" + string(arr1[:])
  fmt.Printf("\n%s",data)
}

答案1

得分: 3

一个字节数组无法转换为字符串,但是字节切片可以:

addr := msg1 + msg2 + msg3 + string(arr1[:])

或者将arr1声明为字节切片:

var arr1 = []byte{11,22,33,44}
英文:

A byte array cannot be converted to a string, but a byte slice can:

addr := msg1 + msg2 + msg3 + string(arr1[:])

Or declare the arr1 as a byte slice:

var arr1 = []byte{11,22,33,44}

答案2

得分: 0

byte 不是 string,请参考 https://en.wikipedia.org/wiki/ASCII

猜测你想要这个:

// var arr1 [4]byte = [4]byte{11, 22, 33, 44}
var arr1 [4]string = [4]string{"11", "22", "33", "44"}

func main() {
	data := "msg1" + arr1[0] + arr1[1] + arr1[2] + arr1[3]
	fmt.Printf("\n%s", data)
}
英文:

byte is not string, read https://en.wikipedia.org/wiki/ASCII .

Guess u want this:

// var arr1 [4]byte = [4]byte{11, 22, 33, 44}
var arr1 [4]string = [4]string{"11", "22", "33", "44"}

func main() {
	data := "msg1" + arr1[0] + arr1[1] + arr1[2] + arr1[3]
	fmt.Printf("\n%s", data)
}

答案3

得分: 0

使用append()函数,我给出了两种方法。我不知道你真正想要哪一种方式,但我希望其中一种对你有帮助。

package main

import "fmt"

var arr [4]string = [4]string{"11", "22", "33", "44"}
var messages [3]string = [3]string{"msg1", "msg2", "msg3"}

func main() {
    // fmt.Println(arr[0:4])

    // 第一种方式
    appending1 := append(messages[0:1], arr[0])
    appending2 := append(messages[1:2], arr[1])
    appending3 := append(messages[2:], arr[2])
    fmt.Println(appending1)
    fmt.Println(appending2)
    fmt.Println(appending3)

    // 第二种方式
    appending1 := append(messages[0:1], arr[0], arr[1], arr[2], arr[3])
    appending2 := append(messages[1:2], arr[0], arr[1], arr[2], arr[3])
    appending3 := append(messages[2:3], arr[0], arr[1], arr[2], arr[3])
    fmt.Println(appending1)
    fmt.Println(appending2)
    fmt.Println(appending3)
}

第一种方式的结果

[11 msg1]
[22 msg2]
[33 msg3]

第二种方式的结果

[msg1 11 22 33 44]
[msg2 11 22 33 44]
[msg3 11 22 33 44]
英文:

Using the append(), I have given you two ways. I don't know which of the way you really want. But I hope one of these ways will be helpful for you.

package main

import "fmt"

var arr [4]string = [4]string{"11", "22", "33", "44"}
var messages [3]string = [3]string{"msg1", "msg2", "msg3"}

func main() {
	// fmt.Println(arr[0:4])

	// One way
	appending1 := append(messages[0:1], arr[0])
	appending2 := append(messages[1:2], arr[1])
	appending3 := append(messages[2:], arr[2])
	fmt.Println(appending1)
	fmt.Println(appending2)
	fmt.Println(appending3)

	// Second way
	appending1 := append(messages[0:1], arr[0], arr[1], arr[2], arr[3])
	appending2 := append(messages[1:2], arr[0], arr[1], arr[2], arr[3])
	appending3 := append(messages[2:3], arr[0], arr[1], arr[2], arr[3])
	fmt.Println(appending1)
	fmt.Println(appending2)
	fmt.Println(appending3)
}

One way result

[11 msg1]
[22 msg2]
[33 msg3]

Second way result

[msg1 11 22 33 44]
[msg2 11 22 33 44]
[msg3 11 22 33 44]

答案4

得分: 0

package main

import (
	"encoding/hex"
	"fmt"
)

var byteArray [4]byte = [4]byte{11, 22, 33, 44}

func main() {
	msg1 := "HI"
	msg2 := "Hello"
	msg3 := "There"

	byteArrayToString := hex.EncodeToString(byteArray[:])
	fmt.Printf("\n%s", msg1+msg2+msg3+byteArrayToString)
}
package main

import (
	"encoding/hex"
	"fmt"
)

var byteArray [4]byte = [4]byte{11, 22, 33, 44}

func main() {
	msg1 := "HI"
	msg2 := "Hello"
	msg3 := "There"

	byteArrayToString := hex.EncodeToString(byteArray[:])
	fmt.Printf("\n%s", msg1+msg2+msg3+byteArrayToString)
}
英文:
package main

import (
	"encoding/hex"
	"fmt"
)

var byteArray [4]byte = [4]byte{11, 22, 33, 44}

func main() {
	msg1 := "HI"
	msg2 := "Hello"
	msg3 := "There"

	byteArrayToString := hex.EncodeToString(byteArray[:])
	fmt.Printf("\n%s", msg1+msg2+msg3+byteArrayToString)
}

huangapple
  • 本文由 发表于 2021年9月14日 12:04:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/69171661.html
匿名

发表评论

匿名网友

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

确定