来自 NSData 的 NSString 字符: fl
如何从包含以下字符的 NSString 的字节日期返回到 NSString: fl ?
NSString *inflatedString01 = @"fl";
// original code that was the problem!
NSData *dataOfString = [NSData dataWithBytes:[inflatedString01 UTF8String] length:[inflatedString length]];
// code that fixes the problem
//NSData *dataOfString = [inflatedString01 dataUsingEncoding:NSUTF8StringEncoding]; //thanks zneak
NSLog(@"%@",inflatedString01);
NSLog(@"%i",[inflatedString01 length]);
NSLog(@"%@",dataOfString);
NSLog(@"%i",[dataOfString length]);
NSString *stringFromData = [NSString stringWithCString:[dataOfString bytes] encoding:NSUTF8StringEncoding]
NSLog(@"%@",stringFromData);
上面的输出给出:
2012-01-02 08:47:48.963 TestApp[74363:fe03] fl
2012-01-02 08:47:49.262 TestApp[74363:fe03] 1
2012-01-02 08:47:49.540 TestApp[74363:fe03] <ef>
2012-01-02 08:47:49.924 TestApp[74363:fe03] 1
2012-01-02 08:47:50.787 TestApp[74363:fe03] (null)
对于最后一个 NSLog 输出,我希望看到 fl 而不是 (null)。我猜测 NSData 的 'ef' 输出具有重要意义。
How can I return back to an NSString from the byte date of the NSString containing this character: fl ?
NSString *inflatedString01 = @"fl";
// original code that was the problem!
NSData *dataOfString = [NSData dataWithBytes:[inflatedString01 UTF8String] length:[inflatedString length]];
// code that fixes the problem
//NSData *dataOfString = [inflatedString01 dataUsingEncoding:NSUTF8StringEncoding]; //thanks zneak
NSLog(@"%@",inflatedString01);
NSLog(@"%i",[inflatedString01 length]);
NSLog(@"%@",dataOfString);
NSLog(@"%i",[dataOfString length]);
NSString *stringFromData = [NSString stringWithCString:[dataOfString bytes] encoding:NSUTF8StringEncoding]
NSLog(@"%@",stringFromData);
The output of the above gives:
2012-01-02 08:47:48.963 TestApp[74363:fe03] fl
2012-01-02 08:47:49.262 TestApp[74363:fe03] 1
2012-01-02 08:47:49.540 TestApp[74363:fe03] <ef>
2012-01-02 08:47:49.924 TestApp[74363:fe03] 1
2012-01-02 08:47:50.787 TestApp[74363:fe03] (null)
I'd like to see fl instead of (null) for the last NSLog output. I'm guessing there is a significance with the 'ef' output of NSData.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是字符大小与二进制大小之间的差异。当你调用
[NSString length]
时,你得到的是字符串中逻辑字符的数量,而不是以任意编码存储它所需的字节数。字符 fl 是 NSString 类的一个逻辑字符,但它的 UTF-8 编码是 ef ac 82:占用 3 个字节。您对
[NSData dataWithBytes:length:]
的调用接收到一个指向这 3 个字节的指针,但随后[inflatedString01 length]
告诉您这只是一个字符,您将其作为字节数;这就是您的数据仅包含ef
的原因。strlen
不支持编码,只会计算 C 字符串中的字节数,直到找到零,因此它将准确返回 UTF-8 字符串中的字节数(就像您在您的评论中指出)。最好的解决方案可能只是简单地调用 [inflatedString01 dataUsingEncoding:NSUTF8StringEncoding] 来获取字节。
The problem is the discrepancy of the character size versus the binary size. When you call
[NSString length]
, what you get is the number of logical characters in the string, not the number of bytes required to store it in an arbitrary encoding. The character fl is one logical character for theNSString
class, but its UTF-8 encoding isef ac 82
: it takes up 3 bytes.Your call to
[NSData dataWithBytes:length:]
receives a pointer to these 3 bytes, but then[inflatedString01 length]
tells that it's only one character and you pass that as the number of bytes; this is why your data contains onlyef
.strlen
, not being encoding-aware, will just count the number of bytes in a C string until it finds a zero, so it will accurately return the number of bytes in an UTF-8 string (as you noted in your comment).The best solution would probably to simply call
[inflatedString01 dataUsingEncoding:NSUTF8StringEncoding]
to get the bytes.