开发者

Getting values out of byte array (UInt8) in C

开发者 https://www.devze.com 2023-01-23 18:31 出处:网络
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

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

精彩评论

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

关注公众号