@@ -1,10 +1,8 @@
package serial
-import (
- "encoding/hex"
- "strings"
-)
+import "encoding/hex"
+// ByteToHexString converts a byte into hex string.
func ByteToHexString(value byte) string {
return hex.EncodeToString([]byte{value})
}
@@ -34,10 +32,22 @@ func BytesToInt64(value []byte) int64 {
int64(value[7])
+// BytesToHexString converts a byte array into hex string.
func BytesToHexString(value []byte) string {
- strs := make([]string, len(value))
- for i, b := range value {
- strs[i] = hex.EncodeToString([]byte{b})
+ m := hex.EncodedLen(len(value))
+ if m == 0 {
+ return "[]"
- return "[" + strings.Join(strs, ",") + "]"
+ n := 1 + m + m/2
+ b := make([]byte, n)
+ hex.Encode(b[1:], value)
+ b[0] = '['
+ for i, j := n-3, m-2+1; i > 0; i -= 3 {
+ b[i+2] = ','
+ b[i+1] = b[j+1]
+ b[i] = b[j]
+ j -= 2
+ }
+ b[n-1] = ']'
+ return string(b)
@@ -0,0 +1,26 @@
+package serial_test
+
+import (
+ "testing"
+ . "v2ray.com/core/common/serial"
+ "v2ray.com/core/testing/assert"
+)
+func TestBytesToHex(t *testing.T) {
+ assert := assert.On(t)
+ cases := []struct {
+ input []byte
+ output string
+ }{
+ {input: []byte{}, output: "[]"},
+ {input: []byte("a"), output: "[61]"},
+ {input: []byte("abcd"), output: "[61,62,63,64]"},
+ {input: []byte(";kdfpa;dfkaepr3ira;dlkvn;vopaehra;dkhf"), output: "[3b,6b,64,66,70,61,3b,64,66,6b,61,65,70,72,33,69,72,61,3b,64,6c,6b,76,6e,3b,76,6f,70,61,65,68,72,61,3b,64,6b,68,66]"},
+ for _, test := range cases {
+ assert.String(test.output).Equals(BytesToHexString(test.input))
+}