本文整理汇总了Golang中encoding/binary.ByteOrder.PutUint32方法的典型用法代码示例。如果您正苦于以下问题:Golang ByteOrder.PutUint32方法的具体用法?Golang ByteOrder.PutUint32怎么用?Golang ByteOrder.PutUint32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类encoding/binary.ByteOrder
的用法示例。
在下文中一共展示了ByteOrder.PutUint32方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: PutUint32
// PutUint32 serializes the provided uint32 using the given byte order into a
// buffer from the free list and writes the resulting four bytes to the given
// writer.
func (l binaryFreeList) PutUint32(w io.Writer, byteOrder binary.ByteOrder, val uint32) error {
buf := l.Borrow()[:4]
byteOrder.PutUint32(buf, val)
_, err := w.Write(buf)
l.Return(buf)
return err
}
示例3: 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
}
示例4: 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
}
示例5: writeIFD
func writeIFD(w io.Writer, ifdOffset int, d []IfdEntry, enc binary.ByteOrder) error {
var buf [ifdLen]byte
// Make space for "pointer area" containing IFD entry data
// longer than 4 bytes.
parea := make([]byte, 1024)
pstart := ifdOffset + ifdLen*len(d) + 6
var o int // Current offset in parea.
// The IFD has to be written with the tags in ascending order.
sort.Sort(ifdSortedByCode(d))
// Write the number of entries in this IFD.
if err := binary.Write(w, enc, uint16(len(d))); err != nil {
return err
}
for _, ent := range d {
enc.PutUint16(buf[0:2], uint16(ent.tag.Code))
enc.PutUint16(buf[2:4], uint16(ent.dataType))
count := uint32(ent.count)
enc.PutUint32(buf[4:8], count)
datalen := int(count * lengths[ent.dataType])
if datalen <= 4 {
for i, b := range ent.rawData {
buf[8+i] = b
}
} else {
if (o + datalen) > len(parea) {
newlen := len(parea) + 1024
for (o + datalen) > newlen {
newlen += 1024
}
newarea := make([]byte, newlen)
copy(newarea, parea)
parea = newarea
}
for i, b := range ent.rawData {
parea[o+i] = b
}
enc.PutUint32(buf[8:12], uint32(pstart+o))
o += datalen
}
if _, err := w.Write(buf[:]); err != nil {
return err
}
}
// The IFD ends with the offset of the next IFD in the file,
// or zero if it is the last one (page 14).
if err := binary.Write(w, enc, uint32(0)); err != nil {
return err
}
_, err := w.Write(parea[:o])
return err
}
示例6: packUint
func packUint(order binary.ByteOrder, v interface{}) (data []byte) {
switch val := v.(type) {
case uint64:
data = make([]byte, 8)
order.PutUint64(data, val)
case uint32:
data = make([]byte, 4)
order.PutUint32(data, val)
case uint16:
data = make([]byte, 2)
order.PutUint16(data, val)
case uint8:
data = []byte{byte(val)}
default:
panic("unsupported type")
}
return data
}
示例7: newSimpleProtocol
func newSimpleProtocol(n int, byteOrder binary.ByteOrder) *simpleProtocol {
protocol := &simpleProtocol{
n: n,
bo: byteOrder,
}
switch n {
case 1:
protocol.encodeHead = func(buffer []byte) {
buffer[0] = byte(len(buffer) - n)
}
protocol.decodeHead = func(buffer []byte) int {
return int(buffer[0])
}
case 2:
protocol.encodeHead = func(buffer []byte) {
byteOrder.PutUint16(buffer, uint16(len(buffer)-n))
}
protocol.decodeHead = func(buffer []byte) int {
return int(byteOrder.Uint16(buffer))
}
case 4:
protocol.encodeHead = func(buffer []byte) {
byteOrder.PutUint32(buffer, uint32(len(buffer)-n))
}
protocol.decodeHead = func(buffer []byte) int {
return int(byteOrder.Uint32(buffer))
}
case 8:
protocol.encodeHead = func(buffer []byte) {
byteOrder.PutUint64(buffer, uint64(len(buffer)-n))
}
protocol.decodeHead = func(buffer []byte) int {
return int(byteOrder.Uint64(buffer))
}
default:
panic("unsupported packet head size")
}
return protocol
}
示例8: WriteFloat32
func WriteFloat32(buf []byte, sample float32, format byte, endianness binary.ByteOrder) {
encoding := format & EncodingMask
if encoding == EncodingFloatingPoint {
endianness.PutUint32(buf, math.Float32bits(sample))
} else {
mult := math.Pow(2, float64(len(buf)*8-1))
var offset float64 = -0.5 // Round instead of floor
if encoding == EncodingUnsignedInt {
offset = mult - 1.5
}
bits := uint32(float64(sample)*mult + offset)
tmp := make([]byte, 4)
endianness.PutUint32(tmp, bits)
if endianness == binary.BigEndian {
copy(buf, tmp[4-len(buf):4])
} else {
copy(buf, tmp)
}
}
}
示例9: Pack
func (s Custom) Pack(buf []byte, order binary.ByteOrder) ([]byte, error) {
order.PutUint32(buf[0:4], uint32(*s.A))
return buf[4:], nil
}
示例10: WriteUint32
func (b *Buffer) WriteUint32(u uint32, order binary.ByteOrder) error {
i := b.Grows(4)
order.PutUint32(b.Buf[i:], u)
return nil
}
示例11: WriteUInt32
// WriteUInt32 writes a uint32 to w.
func WriteUInt32(w io.Writer, byteOrder binary.ByteOrder, value uint32) error {
var buf [4]byte
byteOrder.PutUint32(buf[:], value)
_, err := w.Write(buf[:])
return err
}