将 NSString 转换为字节数组

发布于 2024-10-31 15:20:50 字数 162 浏览 3 评论 0原文

我尝试过谷歌搜索,但没有得到所需的确切答案。

我想在 iPhone 中将 NSString 转换为字节数组。

我还想知道iPhone中NSString转换为byteArray的结果是否与JAVA中字符串转换为byteArray的结果相同?

感谢您提前的帮助...

I have tried googling it but didn't got the exact answer as required.

I want to convert a NSString into Byte Array in iPhone.

Also I want to know that Do the result of conversion of NSString to byteArray in iPhone will be same as conversion of string to byteArray in JAVA?

Thanks for your help in advance...

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

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

发布评论

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

评论(3

无言温柔 2024-11-07 15:20:50

有许多不同的可能的编码。如果您在 Objective-C 和 Java 中使用相同的编码,那么您应该获得相同的字节(或字符串,如果您采用其他方式)。我希望 Java 使用 UTF8,它在 Cocoa 中被指定为 NSUTF8StringEncoding

NSString 中有几种方法可以将字符串转换为一堆字节。其中三个是:-cStringUsingEncoding:-UTF8String-dataUsingEncoding:。第一个返回一个 char * 指向一个以 null 结尾的 char 数组。第二个函数与调用第一个函数并指定 UTF8 的作用几乎相同。第三个返回一个 NSData*,您可以使用 NSData 的 -bytes 方法直接访问字节。

There are many different encodings possible. If you use the same encoding in both Objective-C and in Java, then you should get the same bytes (or string, if you're going the other way). I'd expect Java to use UTF8, which is specified in Cocoa as NSUTF8StringEncoding.

There are several methods in NSString for converting a string to a pile o' bytes. Three of them are: -cStringUsingEncoding:, -UTF8String, and -dataUsingEncoding:. The first returns a char * pointing to a null-terminated array of char. The second does pretty much the same thing as calling the first and specifying UTF8. The third returns a NSData*, and you can access the bytes directly using NSData's -bytes method.

温柔一刀 2024-11-07 15:20:50
NSData* bytes = [str dataUsingEncoding:NSUTF8StringEncoding];

iPhone中NSString转byteArray的结果和JAVA中string转byteArray的结果一样吗?

我相信这是由 UTF-8 标准化的,所以是的。

NSData* bytes = [str dataUsingEncoding:NSUTF8StringEncoding];

Do the result of conversion of NSString to byteArray in iPhone will be same as conversion of string to byteArray in JAVA?

I believe this is standardized by UTF-8, so yes.

以往的大感动 2024-11-07 15:20:50

下面的代码可能对你有帮助。

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
    {
       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
       NSString *str = @"Sample String";
       NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
       NSUInteger len = str.length;
       uint8_t *bytes = (uint8_t *)[data bytes];
       NSMutableString *result = [NSMutableString stringWithCapacity:len * 3];
       [result appendString:@"["];
       int i = 0;
       while(i < len){
       if (i) {
            [result appendString:@","];
       }
            [result appendFormat:@"%d", bytes[i]];
            i++;
       }
       [result appendString:@"]"];
       NSLog (@"String is %@",str);
       NSLog (@"Byte array is %@",result);
       [pool drain];
       return 0;
    }

谢谢,专业开发者

The following code may help you.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
    {
       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
       NSString *str = @"Sample String";
       NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
       NSUInteger len = str.length;
       uint8_t *bytes = (uint8_t *)[data bytes];
       NSMutableString *result = [NSMutableString stringWithCapacity:len * 3];
       [result appendString:@"["];
       int i = 0;
       while(i < len){
       if (i) {
            [result appendString:@","];
       }
            [result appendFormat:@"%d", bytes[i]];
            i++;
       }
       [result appendString:@"]"];
       NSLog (@"String is %@",str);
       NSLog (@"Byte array is %@",result);
       [pool drain];
       return 0;
    }

Thanks, prodeveloper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文