You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/serial/bytes.go

61 lines
1.5 KiB

package serial
import "encoding/hex"
9 years ago
// ByteToHexString converts a byte into hex string.
9 years ago
func ByteToHexString(value byte) string {
return hex.EncodeToString([]byte{value})
}
8 years ago
// BytesToUint16 deserializes a byte array to an uint16 in big endian order. The byte array must have at least 2 elements.
9 years ago
func BytesToUint16(value []byte) uint16 {
return uint16(value[0])<<8 | uint16(value[1])
9 years ago
}
8 years ago
// BytesToUint32 deserializes a byte array to an uint32 in big endian order. The byte array must have at least 4 elements.
func BytesToUint32(value []byte) uint32 {
return uint32(value[0])<<24 |
uint32(value[1])<<16 |
uint32(value[2])<<8 |
uint32(value[3])
}
func BytesToInt(value []byte) int {
return int(value[0])<<24 |
int(value[1])<<16 |
int(value[2])<<8 |
int(value[3])
}
8 years ago
// BytesToInt64 deserializes a byte array to an int64 in big endian order. The byte array must have at least 8 elements.
func BytesToInt64(value []byte) int64 {
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])
}
// BytesToHexString converts a byte array into hex string.
func BytesToHexString(value []byte) string {
m := hex.EncodedLen(len(value))
if m == 0 {
return "[]"
9 years ago
}
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)
9 years ago
}