Convert NSString into byte array iphone

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

Hi I want to convert the NSString into Byte array.

if I have string like "Hello" then it convert into byte like this {65,23,56,56,56}.

thanks

回答1:

Use -[NSString UTF8String] which returns const char*. Read the reference.

A general tip: if you want to find if a class has a method which does something, look up the official reference! I mean, on the web, there's not only Stack Overflow, but the API provider's (i.e. Apple's or Microsoft's) official documentations!



回答2:

NSData *bytes = [test dataUsingEncoding:NSUTF8StringEncoding];


回答3:

From the docs:

"Gets a given range of characters as bytes in a specified encoding."

- (BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger *)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!