如何在golang中将类似“ 11aacc”的字符串转换为十六进制值[] byte(“ \ x11 \ xaa \ cc”)

i want to use UDP to send packet with content like 0x11AACC, "11AACC" is fetched from database, so it's string.

I don't know how to change it to hex value 11AACC, if I use []byte("11AACC")to convert it, it'll change to 6 bytes content.

thanks.

You can use DecodeString from encoding/hex package to convert your hex string to []byte.

example: https://play.golang.org/p/t200M1LqJQ3

package main

import (
    "encoding/hex"
    "fmt"
    "log"
)

func main() {
    s := "11AACC"
    h, err := hex.DecodeString(s)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(h)
}

Link to DecodeString: https://golang.org/pkg/encoding/hex/#DecodeString