UnsafePointer to UInt64


(Rien) #1

What would be the easiest (and fastest) way to convert a pointer to an (unsigned)integer?

Ptr -> String -> Int

seems a bit cumbersome to me :slight_smile:

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl


(Martin R) #2

// Int to pointer:
let ptr = UnsafePointer<Double>(bitPattern: 123)!
print(ptr) // 0x000000000000007b

// Pointer to Int:
let int = Int(bitPattern: ptr)
print(int) // 123

Int has the same size as a pointer on both 32-bit and 64-bit platforms.

Regards, Martin

···

On 16 Feb 2017, at 14:27, Rien via swift-users <swift-users@swift.org> wrote:

What would be the easiest (and fastest) way to convert a pointer to an (unsigned)integer?

Ptr -> String -> Int

seems a bit cumbersome to me :slight_smile:

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #3

Excellent!

I had not seen the bitPattern initializer before…

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

···

On 16 Feb 2017, at 14:47, Martin R <martinr448@gmail.com> wrote:

// Int to pointer:
let ptr = UnsafePointer<Double>(bitPattern: 123)!
print(ptr) // 0x000000000000007b

// Pointer to Int:
let int = Int(bitPattern: ptr)
print(int) // 123

Int has the same size as a pointer on both 32-bit and 64-bit platforms.

Regards, Martin

On 16 Feb 2017, at 14:27, Rien via swift-users <swift-users@swift.org> wrote:

What would be the easiest (and fastest) way to convert a pointer to an (unsigned)integer?

Ptr -> String -> Int

seems a bit cumbersome to me :slight_smile:

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users