SHARE
TWEET

Untitled

a guest Jun 26th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. n := int64(123)
  2.  
  3. fmt.Println(strconv.FormatInt(n, 2)) // 1111011
  4.      
  5. n := int64(123)
  6. fmt.Printf("%b", n)  // 1111011
  7.      
  8. x := big.NewInt(123)
  9. s := fmt.Sprintf("%b", x)
  10. // s == "1111011"
  11.      
  12. /* --- Credit to Dave C in the comments --- */
  13. package main
  14.  
  15. import (
  16.     "bytes"
  17.     "fmt"
  18. )
  19.  
  20. func main() {
  21.     fmt.Printf("<%s>n", fmtBits([]byte{0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0x0D, 0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0x0D}))
  22.  
  23.     // OUTPUT:
  24.     // <11011110 10101101 10111110 11101111 11110000 00001101 11011110 10101101 10111110 11101111 11110000 00001101>
  25. }
  26.  
  27. func fmtBits(data []byte) []byte {
  28.     var buf bytes.Buffer
  29.     for _, b := range data {
  30.         fmt.Fprintf(&buf, "%08b ", b)
  31.     }
  32.     buf.Truncate(buf.Len() - 1) // To remove extra space
  33.     return buf.Bytes()
  34. }
  35.      
  36. package main
  37.  
  38. import (
  39.     "fmt"
  40.     "strconv"
  41.     "unsafe"
  42. )
  43.  
  44. func bInt(n int64) string {
  45.     return strconv.FormatUint(*(*uint64)(unsafe.Pointer(&n)), 2)
  46. }
  47.  
  48. func main() {
  49.     fmt.Println(bInt(-1))
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top