Let's say I have some string as such:
NSString *someString = @"123";
Then, I convert this string to an instance of NSData as such:
NSData *someData = [NSData dataWithBytes:[someString UTF8String] length:[someString length]];
As far as I understand, NSData is essentially just an encoding-less stream of bits. My question is: does NSData determine how many bits are in each UTF8String by checking the value for the length parameter? In other words, [someString UTF8String] returns a C string containing the characters "123", and [someString length] returns the integer 3. Does NSData understand that each character then must consist of 8 bits? Am I totally missing the point?
Thank you.
No.
+ (id)dataWithBytes:(const void *)bytes length:(NSUInteger)length
This method takes simply 2 parameters: void pointer to byte stream in memory and length in the terms how many should be taken into NSData object from this byte stream. This method, as any other, doesn't have a clue and doesn't care, how you got this byte stream and certainly doesn't know anything about UTF8String, it cares only about data types, that they match method signature.
Your idea about how to determine the length of the string is also wrong as Matthias explained. Use strlen C function for that. This function checks upon string termination null character \0.