iPhoneDev
iPhoneDev

Reputation: 228

Convert NSString into byte array iphone

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

Upvotes: 6

Views: 22781

Answers (3)

Aravindhan
Aravindhan

Reputation: 15628

NSData *bytes = [test dataUsingEncoding:NSUTF8StringEncoding];

Upvotes: 5

pkananen
pkananen

Reputation: 1325

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

Upvotes: 0

Yuji
Yuji

Reputation: 34185

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!

Upvotes: 7

Related Questions