开发者

Getting values out of byte array (UInt8) in C

I have a byte array (say, UInt8 *somebuffer) with data in an unknown format. I have tried casting to several datatypes and have not been successful in getting any meaningful data out. What I am trying to do is something like:

float *floatArray = somebuffer;

and then work with the contents as a float, and I have also tried with a few others such as int and double. Is casting in this manner sufficient to access data stored as different types? For example a float would be indexes 0-3 in the byte array but should be index 0 in the float array, correct?

As an aside this happens to be linear PCM audio data, here are the settings I am using to generate the PCM data (I am trying to get levels from the pcm开发者_开发知识库 data to generate a waveform):

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:22000.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

    [recordSetting setValue:[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey];
    [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSetting setValue:[NSNumber numberWithBool:YES] forKey:AVLinearPCMIsFloatKey];


Yes, this is sufficient if you cast to the correct format. Your reasoning is correct, but to get rid of compiler warnings, I would do a cast, like:

float floatArray = (float*)someBuffer;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜