XAgent-Smith

go byte to int

Jan 6th, 2021
940
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×