首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从UnsafeMutableRawPointer中提取字节?

如何从UnsafeMutableRawPointer中提取字节?
EN

Stack Overflow用户
提问于 2016-08-17 03:47:47
回答 2查看 18.2K关注 0票数 29

如何访问字节(或Int16、浮点数等)由C应用程序接口(核心音频等)传递给Swift函数的UnsafeMutableRawPointer ( Swift 3中的新功能)指向的内存不足

EN

回答 2

Stack Overflow用户

发布于 2018-03-03 08:44:39

下面是一个将文字UInt8数组转换为UnsafeMutableRawPointer数组然后再转换回UInt32数组的Swift 4示例

代码语言:javascript
复制
static func unsafePointerTest() {
    //let a : [UInt8] = [0,0,0,4,0,0,0,8,0,0,0,12]
    let a : [UInt8] = [0x04, 0x00, 0x00, 0x00,
                       0x08, 0x00, 0x00, 0x00,
                       0x0C, 0x00, 0x00, 0x00] //little endian
    //0xFF, 0xF0, 0xF0, 0x12]  //317780223 = 12F0F0FF
    let b:UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating:a)
    let bTypedPtr = b.bindMemory(to: UInt32.self, capacity: a.count/4)
    let UInt32Buffer = UnsafeBufferPointer(start: bTypedPtr, count: a.count/4)
    let output = Array(UInt32Buffer)
    print(output)
}
票数 4
EN

Stack Overflow用户

发布于 2016-11-03 04:26:22

下面是UnsafeMutableRawPointer到T/UnsafeMutablePointer转换的接口文档:

代码语言:javascript
复制
/// Binds the allocated memory to type `T` and returns an
/// `UnsafePointer<T>` to the bound memory at `self`.
///
/// - Precondition: The memory is uninitialized.
/// - Postcondition: The memory is bound to 'T' starting at `self` continuing
///   through `self` + `count` * `MemoryLayout<T>.stride`
/// - Warning: Binding memory to a type is potentially undefined if the
///   memory is ever accessed as an unrelated type.
public func bindMemory<T>(to type: T.Type, capacity count: Int) -> UnsafePointer<T>

/// Converts from an `UnsafeRawPointer` to UnsafePointer<T> given that
/// the region of memory starting at `self` is already bound to type `T`.
///
/// - Precondition: The memory is bound to 'T' starting at `self` for some
///   unspecified capacity.
///
/// - Warning: Accessing memory via the returned pointer is undefined if the
///   if the memory has not been bound to `T`.
public func assumingMemoryBound<T>(to: T.Type) -> UnsafePointer<T>

/// Reads raw bytes from memory at `self + offset` and constructs a
/// value of type `T`.
///
/// - Precondition: The underlying pointer plus `offset` is properly
///   aligned for accessing `T`.
///
/// - Precondition: The memory is initialized to a value of some type, `U`,
///   such that `T` is layout compatible with `U`.
public func load<T>(fromByteOffset offset: Int = default, as type: T.Type) -> T

然后可以使用pointeemove apis将Unsafe[MutablePointer]<T>转换为T

代码语言:javascript
复制
/// Accesses the `Pointee` instance referenced by `self`.
///
/// - Precondition: the pointee has been initialized with an instance of
///   type `Pointee`.
public var pointee: Pointee { get }

/// Retrieves the `pointee`, returning the referenced memory to an
/// uninitialized state.
///
/// Equivalent to `{ defer { deinitialize() }; return pointee }()`, but
/// more efficient.
///
/// - Precondition: The pointee is initialized.
///
/// - Postcondition: The memory is uninitialized.
public func move() -> Pointee
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38983277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档