0

PROBLEM:

The problem I am trying to solve is the following. I have audio data recorded by AVAudioRecorder. I can get the NSData by:

NSData *data = [NSData dataWithContentsOfURL: self.audioRecorder.url];

But then I need to convert/reinterpret this NSData to a const char* form which would essentially look like

00 01 00 ff 

which are bytes in hex or at least the equivalent string. They don't have to be actually in hex but just needs to be convertible to hex.

QUESTION:

My question is that the NSData has "\0" in them. So if I do something like this:

    NSUInteger len = [data length];
    Byte *byteData = (Byte*)malloc(len);
    memcpy(byteData, [data bytes], len);

It would not work as the data will be cutoff when it meets the first "\0". I am super new to audio files, but I think it is because of the x00 values in the header. So basically, I don't want to them to be interpreted as "\0" but as "00". Is there a way to do this?

  • Not sure I understand, `'\0'` is the char for `0x00` hex, otherwise known as NULL. Try this `NSLog(@"%.2x", '\0');`, all it does is convert to hex '\0' and log it, you will see they are the same. More fun, `NSLog(@"%.2x %.2x %.2x", '\0', 0x00, 0);` – Popmedic Jul 12 '18 at 18:24
  • Yes, but I need the byte array to continue instead of stopping at a \0 I guess – Mongo Hooli Jul 12 '18 at 18:34
  • The byte array will continue, just when you try to print it as a string it will not... – Popmedic Jul 12 '18 at 18:44

1 Answers1

0

Not sure I understand the question or what you are trying to do. Your memcpy will copy all the bytes to the byteData buffer, it is only when you try to use the byteData buffer as a string (char*) and pass them into a format function (NSLog(%"%s", val)) will it cut off. If you want a string representation of the data as hex:

NSString* bytesToHex(Byte* bytes, NSUInteger count) {
    NSMutableString *hex = [NSMutableString string];
    for(int i = 0; i < count; i++) [hex appendFormat:@"%.2x " , *(bytes+i)];
    return hex;
}

NSString* dataToHex(NSData* data) {
    return bytesToHex((Byte*)data.bytes, data.length);
}

will do it, ie:

Byte* bytes = (Byte*)"t\0h\0i\0s\0 i\0s\0 a\0 t\0e\0st";
NSData* data = [NSData dataWithBytes:bytes length:24];

NSLog(@"%@",NSLog(@"%@", dataToHex(data));

will print:

74 00 68 00 69 00 73 00 20 69 00 73 00 20 61 00 20 74 00 65 00 73 74 00

or

Byte* bytes = (Byte*)"t\0h\0i\0s\0 i\0s\0 a\0 t\0e\0st";
NSData* data = [NSData dataWithBytes:bytes length:24];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
NSLog(@"%@", bytesToHex(byteData, len));

will print:

74 00 68 00 69 00 73 00 20 69 00 73 00 20 61 00 20 74 00 65 00 73 74 00

Just remembered something

Even easier, if you use the NSData description property, it gives you the data in hex already!

Byte* bytes = (Byte*)"t\0h\0i\0s\0 i\0s\0 a\0 t\0e\0st";
NSData* data = [NSData dataWithBytes:bytes length:24];

NSLog(@"%@", data.description);

Will print

<74006800 69007300 20690073 00206100 20740065 00737400>

Not as pretty, but the same thing...

Popmedic
  • 1,791
  • 1
  • 16
  • 21