1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/common/serial/bytes.go

41 lines
811 B
Go
Raw Normal View History

2015-12-12 07:11:49 -05:00
package serial
2016-01-29 04:57:52 -05:00
import (
2016-05-24 15:55:46 -04:00
"encoding/hex"
"strings"
2016-01-29 04:57:52 -05:00
)
2016-05-24 15:55:46 -04:00
func ByteToHexString(value byte) string {
return hex.EncodeToString([]byte{value})
}
func BytesToUint16(value []byte) uint16 {
return uint16(value[0])<<8 + uint16(value[1])
}
2016-05-24 16:09:22 -04:00
func BytesToUint32(value []byte) uint32 {
2016-01-22 11:56:03 -05:00
return uint32(value[0])<<24 +
uint32(value[1])<<16 +
uint32(value[2])<<8 +
uint32(value[3])
}
2016-05-24 16:09:22 -04:00
func BytesToInt64(value []byte) int64 {
2015-12-12 07:11:49 -05:00
return int64(value[0])<<56 +
int64(value[1])<<48 +
int64(value[2])<<40 +
int64(value[3])<<32 +
int64(value[4])<<24 +
int64(value[5])<<16 +
int64(value[6])<<8 +
int64(value[7])
}
2016-01-18 06:58:04 -05:00
2016-05-24 16:09:22 -04:00
func BytesToHexString(value []byte) string {
strs := make([]string, len(value))
for i, b := range value {
strs[i] = hex.EncodeToString([]byte{b})
2016-01-21 07:05:16 -05:00
}
2016-05-24 16:09:22 -04:00
return "[" + strings.Join(strs, ",") + "]"
2016-01-21 07:05:16 -05:00
}