1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
<h1> Package hex </h1> <ul id="short-nav">
<li><code>import "encoding/hex"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul> <h2 id="pkg-overview">Overview </h2> <p>Package hex implements hexadecimal encoding and decoding. </p> <h2 id="pkg-index">Index </h2> <ul id="manual-nav">
<li><a href="#pkg-variables">Variables</a></li>
<li><a href="#AppendDecode">func AppendDecode(dst, src []byte) ([]byte, error)</a></li>
<li><a href="#AppendEncode">func AppendEncode(dst, src []byte) []byte</a></li>
<li><a href="#Decode">func Decode(dst, src []byte) (int, error)</a></li>
<li><a href="#DecodeString">func DecodeString(s string) ([]byte, error)</a></li>
<li><a href="#DecodedLen">func DecodedLen(x int) int</a></li>
<li><a href="#Dump">func Dump(data []byte) string</a></li>
<li><a href="#Dumper">func Dumper(w io.Writer) io.WriteCloser</a></li>
<li><a href="#Encode">func Encode(dst, src []byte) int</a></li>
<li><a href="#EncodeToString">func EncodeToString(src []byte) string</a></li>
<li><a href="#EncodedLen">func EncodedLen(n int) int</a></li>
<li><a href="#NewDecoder">func NewDecoder(r io.Reader) io.Reader</a></li>
<li><a href="#NewEncoder">func NewEncoder(w io.Writer) io.Writer</a></li>
<li><a href="#InvalidByteError">type InvalidByteError</a></li>
<li> <a href="#InvalidByteError.Error">func (e InvalidByteError) Error() string</a>
</li>
</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_Decode">Decode</a></dd> <dd><a class="exampleLink" href="#example_DecodeString">DecodeString</a></dd> <dd><a class="exampleLink" href="#example_Dump">Dump</a></dd> <dd><a class="exampleLink" href="#example_Dumper">Dumper</a></dd> <dd><a class="exampleLink" href="#example_Encode">Encode</a></dd> <dd><a class="exampleLink" href="#example_EncodeToString">EncodeToString</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>hex.go</span> </p> <h2 id="pkg-variables">Variables</h2> <p>ErrLength reports an attempt to decode an odd-length input using <a href="#Decode">Decode</a> or <a href="#DecodeString">DecodeString</a>. The stream-based Decoder returns <span>io.ErrUnexpectedEOF</span> instead of ErrLength. </p>
<pre data-language="go">var ErrLength = errors.New("encoding/hex: odd length hex string")</pre> <h2 id="AppendDecode">func <span>AppendDecode</span> <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func AppendDecode(dst, src []byte) ([]byte, error)</pre> <p>AppendDecode appends the hexadecimally decoded src to dst and returns the extended buffer. If the input is malformed, it returns the partially decoded src and an error. </p>
<h2 id="AppendEncode">func <span>AppendEncode</span> <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func AppendEncode(dst, src []byte) []byte</pre> <p>AppendEncode appends the hexadecimally encoded src to dst and returns the extended buffer. </p>
<h2 id="Decode">func <span>Decode</span> </h2> <pre data-language="go">func Decode(dst, src []byte) (int, error)</pre> <p>Decode decodes src into <a href="#DecodedLen">DecodedLen</a>(len(src)) bytes, returning the actual number of bytes written to dst. </p>
<p>Decode expects that src contains only hexadecimal characters and that src has even length. If the input is malformed, Decode returns the number of bytes decoded before the error. </p> <h4 id="example_Decode"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">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])
</pre> <p>Output:</p> <pre class="output" data-language="go">Hello Gopher!
</pre> <h2 id="DecodeString">func <span>DecodeString</span> </h2> <pre data-language="go">func DecodeString(s string) ([]byte, error)</pre> <p>DecodeString returns the bytes represented by the hexadecimal string s. </p>
<p>DecodeString expects that src contains only hexadecimal characters and that src has even length. If the input is malformed, DecodeString returns the bytes decoded before the error. </p> <h4 id="example_DecodeString"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const s = "48656c6c6f20476f7068657221"
decoded, err := hex.DecodeString(s)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", decoded)
</pre> <p>Output:</p> <pre class="output" data-language="go">Hello Gopher!
</pre> <h2 id="DecodedLen">func <span>DecodedLen</span> </h2> <pre data-language="go">func DecodedLen(x int) int</pre> <p>DecodedLen returns the length of a decoding of x source bytes. Specifically, it returns x / 2. </p>
<h2 id="Dump">func <span>Dump</span> </h2> <pre data-language="go">func Dump(data []byte) string</pre> <p>Dump returns a string that contains a hex dump of the given data. The format of the hex dump matches the output of `hexdump -C` on the command line. </p> <h4 id="example_Dump"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">content := []byte("Go is an open source programming language.")
fmt.Printf("%s", hex.Dump(content))
</pre> <p>Output:</p> <pre class="output" data-language="go">00000000 47 6f 20 69 73 20 61 6e 20 6f 70 65 6e 20 73 6f |Go is an open so|
00000010 75 72 63 65 20 70 72 6f 67 72 61 6d 6d 69 6e 67 |urce programming|
00000020 20 6c 61 6e 67 75 61 67 65 2e | language.|
</pre> <h2 id="Dumper">func <span>Dumper</span> </h2> <pre data-language="go">func Dumper(w io.Writer) io.WriteCloser</pre> <p>Dumper returns a <span>io.WriteCloser</span> that writes a hex dump of all written data to w. The format of the dump matches the output of `hexdump -C` on the command line. </p> <h4 id="example_Dumper"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">lines := []string{
"Go is an open source programming language.",
"\n",
"We encourage all Go users to subscribe to golang-announce.",
}
stdoutDumper := hex.Dumper(os.Stdout)
defer stdoutDumper.Close()
for _, line := range lines {
stdoutDumper.Write([]byte(line))
}
</pre> <p>Output:</p> <pre class="output" data-language="go">00000000 47 6f 20 69 73 20 61 6e 20 6f 70 65 6e 20 73 6f |Go is an open so|
00000010 75 72 63 65 20 70 72 6f 67 72 61 6d 6d 69 6e 67 |urce programming|
00000020 20 6c 61 6e 67 75 61 67 65 2e 0a 57 65 20 65 6e | language..We en|
00000030 63 6f 75 72 61 67 65 20 61 6c 6c 20 47 6f 20 75 |courage all Go u|
00000040 73 65 72 73 20 74 6f 20 73 75 62 73 63 72 69 62 |sers to subscrib|
00000050 65 20 74 6f 20 67 6f 6c 61 6e 67 2d 61 6e 6e 6f |e to golang-anno|
00000060 75 6e 63 65 2e |unce.|
</pre> <h2 id="Encode">func <span>Encode</span> </h2> <pre data-language="go">func Encode(dst, src []byte) int</pre> <p>Encode encodes src into <a href="#EncodedLen">EncodedLen</a>(len(src)) bytes of dst. As a convenience, it returns the number of bytes written to dst, but this value is always <a href="#EncodedLen">EncodedLen</a>(len(src)). Encode implements hexadecimal encoding. </p> <h4 id="example_Encode"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">src := []byte("Hello Gopher!")
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
fmt.Printf("%s\n", dst)
</pre> <p>Output:</p> <pre class="output" data-language="go">48656c6c6f20476f7068657221
</pre> <h2 id="EncodeToString">func <span>EncodeToString</span> </h2> <pre data-language="go">func EncodeToString(src []byte) string</pre> <p>EncodeToString returns the hexadecimal encoding of src. </p> <h4 id="example_EncodeToString"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">src := []byte("Hello")
encodedStr := hex.EncodeToString(src)
fmt.Printf("%s\n", encodedStr)
</pre> <p>Output:</p> <pre class="output" data-language="go">48656c6c6f
</pre> <h2 id="EncodedLen">func <span>EncodedLen</span> </h2> <pre data-language="go">func EncodedLen(n int) int</pre> <p>EncodedLen returns the length of an encoding of n source bytes. Specifically, it returns n * 2. </p>
<h2 id="NewDecoder">func <span>NewDecoder</span> <span title="Added in Go 1.10">1.10</span> </h2> <pre data-language="go">func NewDecoder(r io.Reader) io.Reader</pre> <p>NewDecoder returns an <span>io.Reader</span> that decodes hexadecimal characters from r. NewDecoder expects that r contain only an even number of hexadecimal characters. </p>
<h2 id="NewEncoder">func <span>NewEncoder</span> <span title="Added in Go 1.10">1.10</span> </h2> <pre data-language="go">func NewEncoder(w io.Writer) io.Writer</pre> <p>NewEncoder returns an <span>io.Writer</span> that writes lowercase hexadecimal characters to w. </p>
<h2 id="InvalidByteError">type <span>InvalidByteError</span> </h2> <p>InvalidByteError values describe errors resulting from an invalid byte in a hex string. </p>
<pre data-language="go">type InvalidByteError byte</pre> <h3 id="InvalidByteError.Error">func (InvalidByteError) <span>Error</span> </h3> <pre data-language="go">func (e InvalidByteError) Error() string</pre><div class="_attribution">
<p class="_attribution-p">
© Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
<a href="http://golang.org/pkg/encoding/hex/" class="_attribution-link">http://golang.org/pkg/encoding/hex/</a>
</p>
</div>
|