converting IPv4 string to uint32 (and back again)
Michael Foord
michael.foord at canonical.com
Fri Dec 5 09:32:18 UTC 2014
On 04/12/14 21:07, Gustavo Niemeyer wrote:
> I generally just "hand code" that sort of logic as well:
>
> http://play.golang.org/p/Hvi9t_ZFlw
>
> and yeah, big-endian is the "network byte order". Technically,
> anything would work (XOR it with 42!), but it would be surprising.
>
Thanks guys.
Both those solutions are more elegant than mine, I was converting the
decimal digits into a hex string and then into an integer using strconv
functions. You can do that in one line of Python with list
comprehensions, but it's a bit longer in Go. It also has a bug - the hex
strings need zero padding:
http://pastebin.ubuntu.com/9370769/
Michael
>
>
>
>
> On Thu Dec 04 2014 at 5:50:13 PM Nate Finch <nate.finch at canonical.com
> <mailto:nate.finch at canonical.com>> wrote:
>
> http://play.golang.org/p/H1DI2Bw1OY
>
> Michael was working on translating an ipv4 strings to uint32 (he
> said int, but I hope he meant uint32). He was hand coding it, but
> I figured there was a better way using the built-in libraries.
> Turns out it's not too bad. The code is in the link above. I
> thought it was interesting, so I'm cc'ing juju-dev.
>
> The only thing I wasn't sure about was BigEndian vs.
> LittleEndian. IMO, 0.0.0.5 -> 5 makes the most sense, so I went
> with BigEndian. I suppose it doesn't matter as long as the
> conversions are kept consistent in both directions.
>
> -Nate
> --
> Juju-dev mailing list
> Juju-dev at lists.ubuntu.com <mailto:Juju-dev at lists.ubuntu.com>
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/juju-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/juju-dev/attachments/20141205/b9d4b348/attachment.html>
More information about the Juju-dev
mailing list