endian.go 629 B

123456789101112131415161718192021222324252627282930
  1. package binary
  2. // BigEndian big endian.
  3. var BigEndian bigEndian
  4. type bigEndian struct{}
  5. func (bigEndian) Int8(b []byte) int8 { return int8(b[0]) }
  6. func (bigEndian) PutInt8(b []byte, v int8) {
  7. b[0] = byte(v)
  8. }
  9. func (bigEndian) Int16(b []byte) int16 { return int16(b[1]) | int16(b[0])<<8 }
  10. func (bigEndian) PutInt16(b []byte, v int16) {
  11. b[0] = byte(v >> 8)
  12. b[1] = byte(v)
  13. }
  14. func (bigEndian) Int32(b []byte) int32 {
  15. return int32(b[3]) | int32(b[2])<<8 | int32(b[1])<<16 | int32(b[0])<<24
  16. }
  17. func (bigEndian) PutInt32(b []byte, v int32) {
  18. b[0] = byte(v >> 24)
  19. b[1] = byte(v >> 16)
  20. b[2] = byte(v >> 8)
  21. b[3] = byte(v)
  22. }