Skip to content

Commit 08b21ac

Browse files
committed
Move ToHex/FromHex into bytes
1 parent 9682a3e commit 08b21ac

4 files changed

Lines changed: 43 additions & 42 deletions

File tree

‎common/bytes.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ import (
99
"strings"
1010
)
1111

12+
func ToHex(b []byte) string {
13+
hex := Bytes2Hex(b)
14+
// Prefer output of "0x0" instead of "0x"
15+
if len(hex) == 0 {
16+
hex = "0"
17+
}
18+
return "0x" + hex
19+
}
20+
21+
func FromHex(s string) []byte {
22+
if len(s) > 1 {
23+
if s[0:2] == "0x" {
24+
s = s[2:]
25+
}
26+
if len(s)%2 == 1 {
27+
s = "0" + s
28+
}
29+
return Hex2Bytes(s)
30+
}
31+
return nil
32+
}
33+
1234
type Bytes []byte
1335

1436
func (self Bytes) String() string {

‎common/bytes_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package common
22

33
import (
4+
"bytes"
5+
"testing"
6+
47
checker "gopkg.in/check.v1"
58
)
69

@@ -191,3 +194,21 @@ func (s *BytesSuite) TestRightPadString(c *checker.C) {
191194
c.Assert(resstd, checker.Equals, exp)
192195
c.Assert(resshrt, checker.Equals, val)
193196
}
197+
198+
func TestFromHex(t *testing.T) {
199+
input := "0x01"
200+
expected := []byte{1}
201+
result := FromHex(input)
202+
if bytes.Compare(expected, result) != 0 {
203+
t.Errorf("Expected % x got % x", expected, result)
204+
}
205+
}
206+
207+
func TestFromHexOddLength(t *testing.T) {
208+
input := "0x1"
209+
expected := []byte{1}
210+
result := FromHex(input)
211+
if bytes.Compare(expected, result) != 0 {
212+
t.Errorf("Expected % x got % x", expected, result)
213+
}
214+
}

‎common/common.go‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,6 @@ func DefaultDataDir() string {
6565
}
6666
}
6767

68-
func ToHex(b []byte) string {
69-
hex := Bytes2Hex(b)
70-
// Prefer output of "0x0" instead of "0x"
71-
if len(hex) == 0 {
72-
hex = "0"
73-
}
74-
return "0x" + hex
75-
}
76-
77-
func FromHex(s string) []byte {
78-
if len(s) > 1 {
79-
if s[0:2] == "0x" {
80-
s = s[2:]
81-
}
82-
if len(s)%2 == 1 {
83-
s = "0" + s
84-
}
85-
return Hex2Bytes(s)
86-
}
87-
return nil
88-
}
8968
func IsWindows() bool {
9069
return runtime.GOOS == "windows"
9170
}

‎common/common_test.go‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package common
22

33
import (
4-
"bytes"
54
"math/big"
65
"os"
7-
"testing"
86

97
checker "gopkg.in/check.v1"
108
)
@@ -68,22 +66,3 @@ func (s *CommonSuite) TestLarge(c *checker.C) {
6866
c.Assert(adalarge, checker.Equals, "10000E7 Einstein")
6967
c.Assert(weilarge, checker.Equals, "100 Babbage")
7068
}
71-
72-
//fromHex
73-
func TestFromHex(t *testing.T) {
74-
input := "0x01"
75-
expected := []byte{1}
76-
result := FromHex(input)
77-
if bytes.Compare(expected, result) != 0 {
78-
t.Errorf("Expected % x got % x", expected, result)
79-
}
80-
}
81-
82-
func TestFromHexOddLength(t *testing.T) {
83-
input := "0x1"
84-
expected := []byte{1}
85-
result := FromHex(input)
86-
if bytes.Compare(expected, result) != 0 {
87-
t.Errorf("Expected % x got % x", expected, result)
88-
}
89-
}

0 commit comments

Comments
 (0)