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.

33 lines
635 B

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