File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
1234type Bytes []byte
1335
1436func (self Bytes ) String () string {
Original file line number Diff line number Diff line change 11package common
22
33import (
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+ }
Original file line number Diff line number Diff 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- }
8968func IsWindows () bool {
9069 return runtime .GOOS == "windows"
9170}
Original file line number Diff line number Diff line change 11package common
22
33import (
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- }
You can’t perform that action at this time.
0 commit comments