Advertisement
XAgent-Smith

go byte to int

Jan 6th, 2021
1,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.69 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. import "encoding/binary"
  6. import  "bytes"
  7.  
  8. import "strconv"
  9.  
  10. type IPAddr [4]byte
  11.  
  12. // TODO: Add a "String() string" method to IPAddr.
  13.  
  14. func (self IPAddr) String()string{
  15.     var ip string
  16.     var num uint8
  17.     for index,val:=range self{
  18.         buff:=bytes.NewReader([]byte{val})//creating new read stream
  19.         binary.Read( buff, binary.BigEndian,&num)
  20.            
  21.         if index==len(self)-1{
  22.             ip+=strconv.Itoa(int(num))
  23.            return ip
  24.         }
  25.         ip+=strconv.Itoa(int(num))+"."
  26.     }
  27.    
  28.     return ip
  29. }
  30. func main() {
  31.     hosts := map[string]IPAddr{
  32.         "loopback":  {127, 0, 0, 1},
  33.         "googleDNS": {8, 8, 8, 8},
  34.     }
  35.    
  36.     for name, ip := range hosts {
  37.         fmt.Printf("%v: %v\n", name, ip)
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement