You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
829 B

  1. package util
  2. // big endian
  3. func BytesToUint64(b []byte) (v uint64) {
  4. length := uint(len(b))
  5. for i := uint(0); i < length-1; i++ {
  6. v += uint64(b[i])
  7. v <<= 8
  8. }
  9. v += uint64(b[length-1])
  10. return
  11. }
  12. func BytesToUint32(b []byte) (v uint32) {
  13. length := uint(len(b))
  14. for i := uint(0); i < length-1; i++ {
  15. v += uint32(b[i])
  16. v <<= 8
  17. }
  18. v += uint32(b[length-1])
  19. return
  20. }
  21. func BytesToUint16(b []byte) (v uint16) {
  22. v += uint16(b[0])
  23. v <<= 8
  24. v += uint16(b[1])
  25. return
  26. }
  27. func Uint64toBytes(b []byte, v uint64) {
  28. for i := uint(0); i < 8; i++ {
  29. b[7-i] = byte(v >> (i * 8))
  30. }
  31. }
  32. func Uint32toBytes(b []byte, v uint32) {
  33. for i := uint(0); i < 4; i++ {
  34. b[3-i] = byte(v >> (i * 8))
  35. }
  36. }
  37. func Uint16toBytes(b []byte, v uint16) {
  38. b[0] = byte(v >> 8)
  39. b[1] = byte(v)
  40. }
  41. func Uint8toBytes(b []byte, v uint8) {
  42. b[0] = byte(v)
  43. }