Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "strings"
  6. "unsafe"
  7. )
  8.  
  9. func str2bytes(s string) []byte {
  10. x := (*[2]uintptr)(unsafe.Pointer(&s))
  11. h := [3]uintptr{x[0], x[1], x[1]}
  12. return *(*[]byte)(unsafe.Pointer(&h))
  13. }
  14.  
  15. func bytes2str(b []byte) string {
  16. return *(*string)(unsafe.Pointer(&b))
  17. }
  18.  
  19. func main() {
  20. s := strings.Repeat("abc", 3)
  21. b := str2bytes(s)
  22. s2 := bytes2str(b)
  23. fmt.Println(b, s2)
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement