简体   繁体   中英

On conversion from NSString to NSData

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.

length returns the length of the string, as in "The number of Unicode characters", it does not return the number of bytes in its backing store.

To convert a NSString to NSData you have to use something like this:

NSString *someString = @"123";
NSData *someData = [someString dataUsingEncoding:NSUTF8StringEncoding];

What you do, works for UTF8 that only contains ASCII, because even in UTF8 the ASCII characters takes up only 1 byte. Coincidentally the "logical" length of the string matches the size of its backing store.

Try with a string that contains characters outside of the ASCII range and you will see different results:

NSString *asciiString = @"123";
NSLog(@"\"%@\" - Length: %ld", asciiString, (long)[asciiString length]);
NSData *asciiData = [asciiString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Data length: %ld", [asciiData length]);

NSString *utf8String = @"😡";
NSLog(@"\"%@\" - Length: %ld", utf8String, (long)[utf8String length]);
NSData *utf8Data = [utf8String dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Data length: %ld", [utf8Data length]);

utf8String = @"oö";
NSLog(@"\"%@\" - Length: %ld", utf8String, (long)[utf8String length]);
utf8Data = [utf8String dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Data length: %ld", [utf8Data length]);

Yields:

"123" - Length: 3
Data length: 3
"😡" - Length: 2
Data length: 4
"oö" - Length: 2
Data length: 3

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.

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