本文整理汇总了Golang中encoding/binary.ByteOrder类的典型用法代码### 示例。如果您正苦于以下问题:Golang ByteOrder类的具体用法?Golang ByteOrder怎么用?Golang ByteOrder使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。

在下文中一共展示了ByteOrder类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng