-1

I have this string representing hex:

00000000ff00ff00000900000600020a

I'm trying to convert it to IPv6 with net package

the result I'm expecting is(big endian):

20a:600::9:ff00:ff00::

I tried this:

    ip := "00000000ff00ff00000900000600020a"
    res := make(net.IP, net.IPv6len)
    var err error
    res,err = hex.DecodeString(ip)
    if err != nil {
        fmt.Println("error")
    }
    for i := 0; i < 16/2; i++ {
        res[i], res[16-1-i] = res[16-1-i], res[i]
    }
    fmt.Println(res.String())

but I'm getting this:

a02:6:0:900:ff:ff::

Thanks!

yunger
  • 27
  • 4
  • 1
    is the result you are expecting really correct one? – Jakub Dóka May 03 '21 at 18:18
  • 2
    IPv6 address are resented by 16 bytes; there is no byte order associated with an IPv6 address. The result you are expecting is not a valid IPv6 address so I don't know how to get that from the given format. – JimB May 03 '21 at 18:22
  • 2
    Best I can figure you are actually looking for the address `20a:600:0:9:ff00:ff00::`, which is derived from the reverse ordering of the eight 16-byte groups, rather than reversing each individual byte. This would be quite unusual, so I suggest verifying that the data your are working with is correct to begin with. – JimB May 03 '21 at 18:36
  • I fixed the expected result to 20a:600::9:ff00:ff00:: you right – yunger May 03 '21 at 18:56
  • `20a:600::9:ff00:ff00::` is a RESERVED Ipv6 address that is not allowed to be used. All Global IPv6 addresses are in the `2000::/3` range, and the _[IANA IPv6 Special-Purpose Address Registry](https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml)_ has the valid special purpose ranges, but you address does not fit any. – Ron Maupin May 09 '21 at 17:31

2 Answers2

1

Your question is not clear what is being reversed, when you compare. Typically, when switching endianness, you reverse the bytes, but that is not what you seem to want here.

In any case, here is code to reverse in many different ways, using the IPAddress Go library. Disclaimer: I am the project manager.

str := "00000000ff00ff00000900000600020a"
ipAddr := ipaddr.NewIPAddressString(str).GetAddress()
reversedAddr, _ := ipAddr.ReverseBytes()
reverseEachSegment := ipAddr.ReverseSegments()
reverseBitsEachByte, _ := ipAddr.ReverseBits(true)
reverseBits, _ := ipAddr.ReverseBits(false)
fmt.Println("original", ipAddr)
fmt.Println("bytes reversed", reversedAddr)
fmt.Println("bytes reversed in each segment", reverseEachSegment)
fmt.Println("bits reversed in each byte", reverseBitsEachByte)
fmt.Println("bits reversed", reverseBits)

Output:

original ::ff00:ff00:9:0:600:20a
bytes reversed a02:6:0:900:ff:ff::
bytes reversed in each segment 20a:600:0:9:ff00:ff00::
bits reversed in each byte ::ff00:ff00:90:0:6000:4050
bits reversed 5040:60:0:9000:ff:ff::

For some reason it is reversing the bytes in each segment that gets you what you expect, although that is not switching endianness.

Sean F
  • 4,344
  • 16
  • 30
0

Try this:

for i := 0; i < 16/2; i += 2 {
    res[i], res[16-2-i] = res[16-2-i], res[i]
    res[i+1], res[16-1-i] = res[16-1-i], res[i+1]
}

bytes goes in pair, so you need to flip both at time

XciD
  • 2,537
  • 14
  • 40