Convert NSString into byte array iphone

前端 未结 3 707
滥情空心
滥情空心 2021-01-19 04:10

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

相关标签:
3条回答
  • 2021-01-19 04:44

    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!

    0 讨论(0)
  • 2021-01-19 04:52
    NSData *bytes = [test dataUsingEncoding:NSUTF8StringEncoding];
    
    0 讨论(0)
  • 2021-01-19 05:05

    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
    
    0 讨论(0)
提交回复
热议问题