如何使用 NSString getBytes:maxLength:usedLength:encoding:options:range:remainingRange:

发布于 2024-12-13 14:06:41 字数 763 浏览 0 评论 0原文

我有一个想要作为字节数组的字符串。到目前为止,我已经使用 NSData 来执行此操作:

NSString *message = @"testing";
NSData *messageData = [message dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES];
NSUInteger dataLength = [messageData length];
Byte *byteData = (Byte*)malloc( dataLength );
memcpy( byteData, [messageData bytes], dataLength );

但是,我知道 NSString 有 getBytes:maxLength:usedLength:encoding:options:range:remainingRange: 方法,该方法允许我一起跳过使用 NSData 。我的问题是,我不知道如何正确设置所有参数。

我假设传入的指针数组必须进行 malloc - 但我不确定如何找到要 malloc 的内存量。我知道有 [NSString lengthOfBytesUsingEncoding:][NSString MaximumLengthOfBytesUsingEncoding:] 但我不知道这些是否是我需要使用的方法并且不完全理解他们之间的区别。我认为这与赋予 maxLength 的值相同。其余参数从文档中是有意义的。任何帮助都会很棒。谢谢。

I have a string that I want as a byte array. So far I have used NSData to do this:

NSString *message = @"testing";
NSData *messageData = [message dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES];
NSUInteger dataLength = [messageData length];
Byte *byteData = (Byte*)malloc( dataLength );
memcpy( byteData, [messageData bytes], dataLength );

But, I know that NSString has the getBytes:maxLength:usedLength:encoding:options:range:remainingRange: method that would allow me to skip using NSData all together. My issue is, I don't know how to properly set all the parameters.

I assume the pointer array passed in has to be malloc'ed - but I'm not sure how to find how much memory to malloc. I know there is [NSString lengthOfBytesUsingEncoding:] and [NSString maximumLengthOfBytesUsingEncoding:] but I don't know if those are the methods I need to use and don't fully understand the difference between them. I assume this would be the same value given to maxLength. The rest of the parameters make sense from the documentation. Any help would be great. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-12-20 14:06:41

lengthOfBytesUsingEncoding:maximumLengthOfBytesUsingEncoding: 之间的区别在于,前者精确但速度慢 (O(n)),而后者速度快 (O(1)) 但可能返回比实际需要的字节数要大得多。 maximumLengthOfBytesUsingEncoding: 提供的唯一保证是返回值足够大以包含字符串的字节。

一般来说,你的假设是正确的。所以该方法应该这样使用:

NSUInteger numberOfBytes = [message lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];
void *buffer = malloc(numberOfBytes);
NSUInteger usedLength = 0;
NSRange range = NSMakeRange(0, [message length]);
BOOL result = [message getBytes:buffer maxLength:numberOfBytes usedLength:&usedLength encoding:NSUnicodeStringEncoding options:0 range:range remainingRange:NULL];
...
free(buffer);

The difference between lengthOfBytesUsingEncoding: and maximumLengthOfBytesUsingEncoding: is that the former is exact but slow (O(n)) while the latter is fast (O(1)) but may return a considerably larger number of bytes than is actually needed. The only guarantee that maximumLengthOfBytesUsingEncoding: gives is that the return value will be large enough to contain the string's bytes.

Generally, your assumptions are correct. So the method should be used like this:

NSUInteger numberOfBytes = [message lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];
void *buffer = malloc(numberOfBytes);
NSUInteger usedLength = 0;
NSRange range = NSMakeRange(0, [message length]);
BOOL result = [message getBytes:buffer maxLength:numberOfBytes usedLength:&usedLength encoding:NSUnicodeStringEncoding options:0 range:range remainingRange:NULL];
...
free(buffer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文