How could I convert the following byte array to uintptr? (not uint32 or uint64):
arr := []byte{0xda, 0xcc, 0xd9, 0x74, 0x24, 0xf4}
You can use encoding.binary package:
arr := []byte{0xda, 0xcc, 0xd9, 0x74, 0x24, 0xf4}
for i := 0; i < 8 - len(arr); i++ {
arr = append([]byte{0x0, 0x0}, arr...) // for not to get index out of range
}
ptr := binary.BigEndian.Uint64(arr)
fmt.Printf("0x%x\n", uintptr(ptr))
Assuming that uintptr is 64-bits, and that you want a big-endian encoding, you can quite easily construct the right value even without delving into the standard library's binary package.
package main
import "fmt"
func main() {
arr := []byte{0xda, 0xcc, 0xd9, 0x74, 0x24, 0xf4}
var r uintptr
for _, b := range arr {
r = (r << 8) | uintptr(b)
}
fmt.Printf("%x", r)
}
This code outputs daccd97424f4 if you're on a 64-bit int version of go (and not, for example, on the go playground).
**(**uintptr)(unsafe.Pointer(&byte_slice))uintptr(uint_variable)0x0000f42474d9ccdaor0x0000daccd97424f4. My first bet would be the latter (because0xdais only 2-byte aligned) but it's still not really clear: if it is the latter, excising the first two00bytes is weird.