github.com/quantosnetwork/Quantos@v0.0.0-20220306172517-e20b28c5a29a/crypto/bytes.go (about) 1 package crypto 2 3 import ( 4 "encoding/binary" 5 "reflect" 6 "unsafe" 7 ) 8 9 func BytesToString(b []byte) string { 10 return *(*string)(unsafe.Pointer(&b)) 11 } 12 13 func StringToBytes(s string) (b []byte) { 14 strh := (*reflect.StringHeader)(unsafe.Pointer(&s)) 15 sh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) 16 sh.Data = strh.Data 17 sh.Len = strh.Len 18 sh.Cap = strh.Len 19 return b 20 } 21 22 const maxStartEndStringLen = 80 23 24 func StartEndString(s string) string { 25 if len(s) <= maxStartEndStringLen { 26 return s 27 } 28 start := s[:40] 29 end := s[len(s)-40:] 30 return start + "..." + end 31 } 32 33 func BytesToInt(b []byte) int { 34 return int(binary.LittleEndian.Uint32(b)) 35 }