How to Convert a Unsafemutablepointer<Void> to Uint8

How do you convert a UnsafeMutablePointer Void to UInt8?

You have to convert the pointer to the correct type first
(a pointer to UInt8) and then you can access the memory it points to:

let u8 = UnsafePointer<UInt8>(theUnsafeMutablePointerVar).memory

In Swift 3, a void pointer from C is imported to Swift as
UnsafeMutableRawPointer, and one can read the pointed-to data
with

let u8 = theUnsafeMutablePointerVar.load(as: UInt8.self)

Convert UnsafeMutablePointer Int16 to UInt8

Casting an Unsafe(Mutable)Pointer<Int16> to UnsafePointer<Int8>
would simply be:

let buffer: UnsafeMutablePointer<Int16> = ...
let count: Int = ... // # of Int16 values

let result = buffer.withMemoryRebound(to: UInt8.self, capacity: 2 * count) {
outputStream.write($0, maxLength: 2 * count)
}

Cast UnsafeMutablePointer Void to my struct type

Just a guess referencing from here

struct YourStruct {
var name : String
}
var structInstance = YourStruct.init(name: "Jose")

func delegateMethod(voidPtr : UnsafePointer<Void>) {
//CONVERSION HERE
let myStruct = UnsafePointer<YourStruct>(voidPtr).memory
print("\(myStruct.name)")
}
delegateMethod(&structInstance)

UnsafeMutablePointer UInt8 to [UInt8] without memory copy

As already mentioned in the comments, you can create an
UnsafeMutableBufferPointer from the pointer:

let a = UnsafeMutableBufferPointer(start: p, count: n)

This does not copy the data, which means that you have to ensure that
the pointed-to data is valid as long as a is used.
Unsafe (mutable) buffer pointers have similar access methods like arrays,
such as subscripting:

for i in 0 ..< a.count {
print(a[i])
}

or enumeration:

for elem in a {
print(elem)
}

You can create a "real" array from the buffer pointer with

let b = Array(a)

but this will copy the data.

Here is a complete example demonstrating the above statements:

func test(_ p : UnsafeMutablePointer<UInt8>, _ n : Int) {

// Mutable buffer pointer from data:
let a = UnsafeMutableBufferPointer(start: p, count: n)
// Array from mutable buffer pointer
let b = Array(a)

// Modify the given data:
p[2] = 17

// Printing elements of a shows the modified data: 1, 2, 17, 4
for elem in a {
print(elem)
}

// Printing b shows the orignal (copied) data: 1, 2, 3, 4
print(b)

}

var bytes : [UInt8] = [ 1, 2, 3, 4 ]
test(&bytes, bytes.count)

Swift convert [UIn16] to UnsafeMutablePointer UInt16

The withXXXBytes methods get you raw pointers, but here you want a typed pointer. Use withUnsafeMutableBufferPointer instead and get the base address:

arrayOfUInt16.withUnsafeMutableBufferPointer { (bufferPointer) in
let mutablePointerOfUInt16 = bufferPointer.baseAddress
// do whatever you want with mutablePointerOfUInt16
// just don't "store or return the pointer for later use." as the documentation says
// because the pointer is only valid within this block
}

How to convert an UnsafeMutablePointer to a Data object

You can use:

let data = Data(bytes: bytes, count: 20)

Casting between different UnsafePointer T in swift

struct UnsafePointer<T> has a constructor

/// Convert from a UnsafePointer of a different type.
///
/// This is a fundamentally unsafe conversion.
init<U>(_ from: UnsafePointer<U>)

which you can use here

doThingsOnRawData(UnsafePointer<UInt8>(data.bytes))

You can even omit the generic type because it is inferred from the context:

doThingsOnRawData(UnsafePointer(data.bytes))

Update for Swift 3: As of Xcode 8 beta 6, you cannot convert
directly between different unsafe pointers anymore.

For data: NSData, data.bytes is a UnsafeRawPointer which can
be converted to UnsafePointer<UInt8> with assumingMemoryBound:

doThingsOnRawData(data.bytes.assumingMemoryBound(to: UInt8.self))

For data: Data it is even simpler:

data.withUnsafeBytes { doThingsOnRawData($0) }

What is UnsafeMutablePointer Void ? How to modify the underlying memory?

From the docs for SKMutableTexture.modifyPixelDataWithBlock:

The texture bytes are assumed to be stored as tightly packed 32 bpp, 8bpc (unsigned integer) RGBA pixel data. The color components you provide should have already been multiplied by the alpha value.

So, while you’re given a void*, the underlying data is in the form of a stream of 4x8 bits.

You could manipulate such a structure like so:

// struct of 4 bytes
struct RGBA {
var r: UInt8
var g: UInt8
var b: UInt8
var a: UInt8
}

let tex = SKMutableTexture(size: CGSize(width: 10, height: 10))
tex.modifyPixelDataWithBlock { voidptr, len in
// convert the void pointer into a pointer to your struct
let rgbaptr = UnsafeMutablePointer<RGBA>(voidptr)

// next, create a collection-like structure from that pointer
// (this second part isn’t necessary but can be nicer to work with)
// note the length you supply to create the buffer is the number of
// RGBA structs, so you need to convert the supplied length accordingly...
let pixels = UnsafeMutableBufferPointer(start: rgbaptr, count: Int(len / sizeof(RGBA))

// now, you can manipulate the pixels buffer like any other mutable collection type
for i in indices(pixels) {
pixels[i].r = 0x00
pixels[i].g = 0xff
pixels[i].b = 0x00
pixels[i].a = 0x20
}
}


Related Topics



Leave a reply



Submit