v2ray-core/common/serial/bytes.go

41 lines
811 B
Go
Raw Normal View History

2015-12-12 12:11:49 +00:00
package serial
2016-01-29 09:57:52 +00:00
import (
2016-05-24 19:55:46 +00:00
"encoding/hex"
"strings"
2016-01-29 09:57:52 +00:00
)
2016-05-24 19:55:46 +00:00
func ByteToHexString(value byte) string {
return hex.EncodeToString([]byte{value})
}
func BytesToUint16(value []byte) uint16 {
2016-06-26 20:34:48 +00:00
return uint16(value[0])<<8 | uint16(value[1])
2016-05-24 19:55:46 +00:00
}
2016-05-24 20:09:22 +00:00
func BytesToUint32(value []byte) uint32 {
2016-06-26 20:34:48 +00:00
return uint32(value[0])<<24 |
uint32(value[1])<<16 |
uint32(value[2])<<8 |
2016-01-22 16:56:03 +00:00
uint32(value[3])
}
2016-05-24 20:09:22 +00:00
func BytesToInt64(value []byte) int64 {
2016-06-26 20:34:48 +00: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 |
2015-12-12 12:11:49 +00:00
int64(value[7])
}
2016-01-18 11:58:04 +00:00
2016-05-24 20:09:22 +00: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 12:05:16 +00:00
}
2016-05-24 20:09:22 +00:00
return "[" + strings.Join(strs, ",") + "]"
2016-01-21 12:05:16 +00:00
}