saleks28

pasoib8_convertion

Dec 15th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.48 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "net"
  5.     "strconv"
  6.     "strings"
  7. )
  8.  
  9. //StringToUint8 convertion from string to uint8
  10. func StringToUint8(str string) uint8 {
  11.     bufVal, _ := strconv.ParseUint(str, 10, 8)
  12.     return uint8(bufVal)
  13. }
  14.  
  15. //StringToUint16 convertion from string to uint16
  16. func StringToUint16(str string) uint16 {
  17.     bufVal, _ := strconv.ParseUint(str, 10, 16)
  18.     return uint16(bufVal)
  19. }
  20.  
  21. //StringToUint32 convertion from string to uint16
  22. func StringToUint32(str string) uint32 {
  23.     bufVal, _ := strconv.ParseUint(str, 10, 32)
  24.     return uint32(bufVal)
  25. }
  26.  
  27. // //StringToIP convertion from string to []byte for IP
  28. // func StringToIP(str string) net.IP {
  29. //  strSplit := strings.Split(str, ".")
  30. //  var ipParts [4]byte
  31. //  for idx, i := range strSplit {
  32. //      buf, _ := strconv.ParseInt(i, 10, 16)
  33. //      ipParts[idx] = byte(buf)
  34. //  }
  35. //  return net.IPv4(ipParts[0], ipParts[1], ipParts[2], ipParts[3])
  36. // }
  37.  
  38. //StringToHardAddr convertion from string to net.Hardware address
  39. func StringToHardAddr(str string) net.HardwareAddr {
  40.     bytes := strings.Split(str, ":")
  41.     var macParts [6]byte
  42.     for idx, i := range bytes {
  43.         buf, _ := strconv.ParseUint(i, 16, 32)
  44.         macParts[idx] = byte(buf)
  45.     }
  46.     return net.HardwareAddr{macParts[0], macParts[1], macParts[2],
  47.         macParts[3], macParts[4], macParts[5]}
  48. }
  49.  
  50. //RadioToBool convertion from radio tag value to bool value
  51. func RadioToBool(str string) bool {
  52.     if str == "on" {
  53.         return true
  54.     } else if str == "of" {
  55.         return false
  56.     }
  57.     return false
  58. }
Add Comment
Please, Sign In to add comment