1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/serial/numbers.go

68 lines
1.5 KiB
Go
Raw Normal View History

2015-12-03 10:57:23 -05:00
package serial
2016-12-09 07:17:34 -05:00
import "strconv"
2017-05-02 16:23:07 -04:00
import "io"
2015-12-03 10:57:23 -05:00
2018-04-02 03:52:16 -04:00
// Uint16ToBytes serializes a uint16 into bytes in big endian order.
2016-06-26 16:34:48 -04:00
func Uint16ToBytes(value uint16, b []byte) []byte {
return append(b, byte(value>>8), byte(value))
2016-05-24 15:55:46 -04:00
}
func Uint16ToString(value uint16) string {
return strconv.Itoa(int(value))
}
2017-05-02 16:23:07 -04:00
func ReadUint16(reader io.Reader) (uint16, error) {
var b [2]byte
if _, err := io.ReadFull(reader, b[:]); err != nil {
return 0, err
}
return BytesToUint16(b[:]), nil
}
2016-12-09 07:17:34 -05:00
func WriteUint16(value uint16) func([]byte) (int, error) {
2016-12-09 06:08:25 -05:00
return func(b []byte) (int, error) {
2017-08-25 09:11:06 -04:00
Uint16ToBytes(value, b[:0])
2016-12-09 06:08:25 -05:00
return 2, nil
2016-12-06 05:03:42 -05:00
}
}
2016-06-26 16:34:48 -04:00
func Uint32ToBytes(value uint32, b []byte) []byte {
return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
}
2016-06-27 16:22:01 -04:00
func Uint32ToString(value uint32) string {
return strconv.FormatUint(uint64(value), 10)
}
2016-12-09 07:17:34 -05:00
func WriteUint32(value uint32) func([]byte) (int, error) {
2016-12-09 06:08:25 -05:00
return func(b []byte) (int, error) {
2017-08-25 09:11:06 -04:00
Uint32ToBytes(value, b[:0])
2016-12-09 06:08:25 -05:00
return 4, nil
2016-12-06 05:03:42 -05:00
}
}
2016-06-26 16:34:48 -04:00
func IntToBytes(value int, b []byte) []byte {
return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
2016-05-23 14:16:31 -04:00
}
2016-05-24 16:15:46 -04:00
func IntToString(value int) string {
return Int64ToString(int64(value))
2015-12-12 07:11:49 -05:00
}
2016-06-26 16:34:48 -04:00
func Int64ToBytes(value int64, b []byte) []byte {
return append(b,
byte(value>>56),
byte(value>>48),
byte(value>>40),
byte(value>>32),
byte(value>>24),
byte(value>>16),
byte(value>>8),
byte(value))
2015-12-12 07:11:49 -05:00
}
2016-05-24 16:15:46 -04:00
func Int64ToString(value int64) string {
return strconv.FormatInt(value, 10)
}