[SOLVED] Convert two 8 byte unsigned QWords to in6_addr (IPv6) lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Gizmo · 2016-10-28 16:09 Hi I need to convert the binary representation of an IPv6 address into the human readable and compressed format. At the moment, the values are stored as two 8-byte unsigned QWord values (the first 8 bytes, and the second 8 bytes, are two seperate QWords, totalling a 16 byte range). For example : 20 03 65 BC A0 C1 F4 10 38 8E 9C FF FE B6 D1 C0 is the address as raw bytes. QWordA : holds the 1st part 20 03 65 BC A0 C1 F4 10 QWordB : holds the 2nd part 38 8E 9C FF FE B6 D1 C0 I am aware of the sockets unit (http://www.freepascal.org/docs- html/rtl/sockets/index-5.html) and specifically the function NetAddrToStr6 (http://www.freepascal.org/docs- html/rtl/sockets/netaddrtostr6.html) which I think is what I need to use. But I am confused by how to get QWordA and QWordB to it using type in6_addr (http://www.freepascal.org/docs- html/rtl/sockets/in6_addr.html) I cannot simply declare a variable of type in6_addr and assign QWordA and QWordB to it, because it expects bytes. IN the short term I have written a function that loops over the string representation of the hex bytes, removes any consequitve zeroes (groups of 4) and replaces with ':') but that is not ideal (slower and perhaps not even exactly exactly correct) Can anyone guide me on the issue? Thanks ---------------------------------------------------------------------- Remy Lebeau · 2016-10-28 20:42 Quote from: Gizmo on October 28, 2016, 06:09:22 pmI need to convert the binary representation of an IPv6 address into the human readable and compressed format. Use standard socket APIs for that, such as inet_ntop(), or getnameinfo() with the NI_NUMERICHOST flag. Quote from: Gizmo on October 28, 2016, 06:09:22 pmI cannot simply declare a variable of type in6_addr and assign QWordA and QWordB to it, because it expects bytes. Sure, you can. You can use Move() or similar function to move raw bytes from one memory buffer to another.  Use your QWord variables as input buffers and your in6_addr as an output buffer. Or, you can simply type-cast the memory address of the in6_addr variable to PQWord, and then access the in6_addr as if it were a static 2-qword array using pointer arithmetic, then you can assign your QWord variables directly to it. Or, simply stop storing your IP addresses in QWord variables to begin with.  Why are you doing that at all?  Why not use in6_addr or event sockaddr_in6 to begin with?  Or at least a 16-byte array?  Where are your IP addresses coming from? ---------------------------------------------------------------------- Gizmo · 2016-10-30 23:26 Thanks Remy! I've not used "Move" before, so that was a very useful tip. Yes, it worked perfectly, just passing 16 bytes from Position X to Move and shifting them into a in6_addr declared variable worked first time! You're a diamond.