當前位置: 首頁>>代碼示例>>Golang>>正文


Golang binary.ByteOrder類代碼示例

本文整理匯總了Golang中encoding/binary.ByteOrder的典型用法代碼示例。如果您正苦於以下問題:Golang ByteOrder類的具體用法?Golang ByteOrder怎麽用?Golang ByteOrder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ByteOrder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: toUTF16

func toUTF16(data []byte) ([]uint16, error) {
	if len(data) < 2 {
		return []uint16{}, nil
	}
	if len(data)%2 > 0 {
		// TODO: if this is UTF-16 BE then this is likely encoded wrong
		data = append(data, 0)
	}

	var bo binary.ByteOrder

	if data[0] == 0xFF && data[1] == 0xFE {
		// UTF-16 LE
		bo = binary.LittleEndian
	} else if data[0] == 0xFE && data[1] == 0xFF {
		// UTF-16 BE
		bo = binary.BigEndian
	} else {
		return []uint16{}, nil
	}

	s := make([]uint16, 0, len(data)/2)
	for i := 2; i < len(data); i += 2 {
		s = append(s, bo.Uint16(data[i:i+2]))
	}
	return s, nil
}
開發者ID:bobertlo,項目名稱:go-id3,代碼行數:27,代碼來源:util.go

示例2: ReadUInt32

// ReadUInt32 reads a uint32 from r.
func ReadUInt32(r io.Reader, byteOrder binary.ByteOrder) (uint32, error) {
	var buf [4]byte
	if _, err := io.ReadFull(r, buf[:]); err != nil {
		return 0, err
	}
	return byteOrder.Uint32(buf[:]), nil
}
開發者ID:dgraph-io,項目名稱:dgraph,代碼行數:8,代碼來源:binary.go

示例3: ReadFloat32

func ReadFloat32(buf []byte, format byte, endianness binary.ByteOrder) float32 {
	encoding := format & EncodingMask

	if encoding == EncodingFloatingPoint {
		return math.Float32frombits(endianness.Uint32(buf))
	} else {
		offset := 0
		if endianness == binary.LittleEndian {
			offset = len(buf) - 1
		}
		var neg byte = 0
		if encoding == EncodingSignedInt && buf[offset]&(1<<7) != 0 {
			neg = 0xFF
		}
		tmp := []byte{neg, neg, neg, neg}

		if endianness == binary.BigEndian {
			copy(tmp[4-len(buf):], buf)
		} else {
			copy(tmp, buf)
		}

		sample := endianness.Uint32(tmp)

		div := math.Pow(2, float64(len(buf)*8-1))
		if encoding == EncodingSignedInt {
			return float32(float64(int32(sample)) / div)
		} else {
			return float32(float64(sample)/div - 1.0)
		}
	}
}
開發者ID:xthexder,項目名稱:rawstreamer,代碼行數:32,代碼來源:common.go

示例4: PutUint64

// PutUint64 serializes the provided uint64 using the given byte order into a
// buffer from the free list and writes the resulting eight bytes to the given
// writer.
func (l binaryFreeList) PutUint64(w io.Writer, byteOrder binary.ByteOrder, val uint64) error {
	buf := l.Borrow()[:8]
	byteOrder.PutUint64(buf, val)
	_, err := w.Write(buf)
	l.Return(buf)
	return err
}
開發者ID:decred,項目名稱:dcrd,代碼行數:10,代碼來源:common.go

示例5: OpenRawDataFile

// Open a CDataFile
func OpenRawDataFile(name string, readOnly bool, byteOrder binary.ByteOrder, byteAlignment uint64, valueSize int) (*RawDataFile, error) {
	flag := os.O_RDWR
	if readOnly {
		flag = os.O_RDONLY
	}

	file, err := os.OpenFile(name, flag, 0644)
	if err != nil {
		return nil, err
	}

	var univalToBytes func([]byte, unival)
	var bytesToUnival func([]byte) unival
	switch valueSize {
	case 8:
		univalToBytes = func(dst []byte, src unival) {
			byteOrder.PutUint64(dst, src.AsUnsignedLong())
		}
		bytesToUnival = func(src []byte) unival {
			return unival(byteOrder.Uint64(src))
		}
	default:
		return nil, errors.Errorf("Invalid value size %d", valueSize)
	}
	return &RawDataFile{
		file: file,
		//		byteOrder:     byteOrder,
		byteAlignment: byteAlignment,
		valueSize:     valueSize,
		univalToBytes: univalToBytes,
		bytesToUnival: bytesToUnival,
	}, nil
}
開發者ID:untoldwind,項目名稱:gorrd,代碼行數:34,代碼來源:raw_data_file.go

示例6: NewNullFrame

func NewNullFrame(data []byte, byteOrder binary.ByteOrder) (*NullFrame, error) {
	if len(data) < NULL_FRAME_HEADER_LENGTH {
		return nil, errors.New(fmt.Sprintf("required at least %d bytes of data.", NULL_FRAME_HEADER_LENGTH))
	}

	return &NullFrame{byteOrder.Uint32(data), data[4:]}, nil
}
開發者ID:justmao945,項目名稱:httpdump,代碼行數:7,代碼來源:null.go

示例7: durationToBytes

func durationToBytes(d time.Duration, order binary.ByteOrder) []byte {
	buf := make([]byte, 8)
	nsec := d.Nanoseconds()
	order.PutUint32(buf, uint32(nsec/nanosPerSec))
	order.PutUint32(buf[4:], uint32(nsec%nanosPerSec))
	return buf
}
開發者ID:liamstask,項目名稱:go-rtps,代碼行數:7,代碼來源:time.go

示例8: packParamString

func packParamString(bin binary.ByteOrder, s string) []byte {
	b := make([]byte, (4+len(s)+1+3) & ^0x3) // must be 32-bit aligned
	bin.PutUint32(b[0:], uint32(len(s)+1))
	copy(b[4:], []byte(s))
	b[4+len(s)] = 0
	return b
}
開發者ID:liamstask,項目名稱:go-rtps,代碼行數:7,代碼來源:proto.go

示例9: ReadUint32

func (b *Buffer) ReadUint32(order binary.ByteOrder) (uint32, error) {
	if b.readPos >= len(b.Buf)-4 {
		return 0, io.EOF
	}
	u := order.Uint32(b.Buf[b.readPos:])
	b.readPos += 4
	return u, nil
}
開發者ID:huangjiasingle,項目名稱:gohper,代碼行數:8,代碼來源:buffer.go

示例10: ReadUint16

func (b *Buffer) ReadUint16(order binary.ByteOrder) (uint16, error) {
	if b.readPos >= len(b.buf)-2 {
		return 0, io.EOF
	}
	u := order.Uint16(b.buf[b.readPos:])
	b.readPos += 2
	return u, nil
}
開發者ID:sshitaime,項目名稱:gohper,代碼行數:8,代碼來源:buffer.go

示例11: benchUpdate

func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
	trie := newEmpty()
	k := make([]byte, 32)
	for i := 0; i < b.N; i++ {
		e.PutUint64(k, uint64(i))
		trie.Update(k, k)
	}
	return trie
}
開發者ID:Cisko-Rijken,項目名稱:go-expanse,代碼行數:9,代碼來源:trie_test.go

示例12: newQosHistoryFromBytes

func newQosHistoryFromBytes(bin binary.ByteOrder, b []byte) (qosHistory, error) {
	if len(b) < 4+4 {
		return qosHistory{}, io.EOF
	}
	return qosHistory{
		kind:  bin.Uint32(b[0:]),
		depth: bin.Uint32(b[4:]),
	}, nil
}
開發者ID:liamstask,項目名稱:go-rtps,代碼行數:9,代碼來源:qos.go

示例13: timeFromBytes

func timeFromBytes(order binary.ByteOrder, b []byte) (time.Time, error) {
	if len(b) < 8 {
		return timeInvalid, io.EOF
	}

	sec := int64(order.Uint32(b[0:]))
	frac := int64(order.Uint32(b[4:]))
	return time.Unix(sec, (frac*nanosPerSec)>>32).UTC(), nil
}
開發者ID:liamstask,項目名稱:go-rtps,代碼行數:9,代碼來源:time.go

示例14: parseUtf16

func parseUtf16(strBytes []byte, bo binary.ByteOrder) (string, error) {
	shorts := make([]uint16, 0, len(strBytes)/2)
	for i := 0; i < len(strBytes); i += 2 {
		short := bo.Uint16(strBytes[i : i+2])
		shorts = append(shorts, short)
	}

	return string(utf16.Decode(shorts)), nil
}
開發者ID:camarox53,項目名稱:coreos-baremetal,代碼行數:9,代碼來源:util.go

示例15: timeToBytes

func timeToBytes(t time.Time, order binary.ByteOrder) []byte {
	sec := uint32(t.Unix())
	frac := uint32((nanosPerSec - 1 + (int64(t.Nanosecond()) << 32)) / nanosPerSec)

	b := make([]byte, 8)
	order.PutUint32(b[0:], sec)
	order.PutUint32(b[4:], frac)
	return b
}
開發者ID:liamstask,項目名稱:go-rtps,代碼行數:9,代碼來源:time.go


注:本文中的encoding/binary.ByteOrder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。