简体   繁体   中英

How to convert multiple-character numeric format into a readable string in Swift?

I'm trying to convert AudioStreamBasicDescription's mFormatID property, which I get with AudioFileGetGlobalInfo, to a readable string. In Objective-C it looks like this:

for (int i = 0; i < asbdCount; i++) {
  UInt32 format4cc = CFSwapInt32HostToBig(asbds[i].mFormatID);
  NSLog(@"mFormatID: %4.4s", (char*)&format4cc);
}

This code is a piece of CAStreamFormatTester from the Learning Core Audio book. asbds is a pointer to AudioStreamBasicDescriptions. How to convert this into Swift?

If asbds is of type UnsafePointer<AudioStreamBasicDescription> or of type [AudioStreamBasicDescription] , then I believe this should work:

for i in 0 ..< asbdCount {
    var format4cc = CFSwapInt32HostToBig(asbds[i].mFormatID)
    withUnsafePointer(&format4cc) { cptr in
        println(String(format: "mFormatID: %4.4s", cptr))
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM