hex

最后更新于:2022-04-02 02:43:31

[TOC] ## Encode / EncodetoString ``` // 方式一 src := []byte("Hello") encodedStr := hex.EncodeToString(src) fmt.Printf("%s\n", encodedStr) // 48656c6c6f // 方式二 dst := make([]byte, hex.EncodedLen(len(src))) hex.Encode(dst, src) fmt.Printf("%+v\n", string(dst)) // 48656c6c6f ``` ## Decode ``` src := []byte("48656c6c6f20476f7068657221") dst := make([]byte, hex.DecodedLen(len(src))) n, err := hex.Decode(dst, src) if err != nil { log.Fatal(err) } fmt.Printf("%s\n", dst[:n]) ``` ## DecodeString ``` const s = "48656c6c6f20476f7068657221" decoded, err := hex.DecodeString(s) if err != nil { log.Fatal(err) } fmt.Printf("%s\n", decoded) // Hello Gopher! ``` ## hex.Dump ``` content := []byte("1234567890abcdefghijk") fmt.Printf("%s", hex.Dump(content)) // Output: //00000000 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 |1234567890abcdef| //00000010 67 68 69 6a 6b |ghijk| ```
';