ios - Process unicode string in C and Objective C -


I type the C function to read the characters in the user-input string. Since this string is user-input, so it can contain any unicode characters. There is an Objective C method that receives the user input NSString, then convert this string to NSData and pass this data to the C function for processing. The C function searches for these symbols: *, [,], _, does not care about any other characters, every time it gets a symbol, it processes and then calls an objective method, the symbol Passes the place.

C code:

  typedef zero (* callback) (zero * reference, size_t location); Zero process (const uint8_t * data, size_ty length, callback CB, zero * reference) {size_t i = 0; While (i & lt; length) {if (data [i] == '*' || data [i] == '['] data [i] == ']' || data [i] == ' _ ') {Int Valid = 0; // Do something, set valid = 1 if (valid) CB (references, i); } I ++; }}  

Objective C code:

  // declared a C function. M file void mycallback (zero * reference, size_t location) {[bridge bridge id) reference process synchronize location: location]; } - (zero) Process Simbolite location: (NSInteger) location {NSString * result = [self.string substringWithRange: NSMakeRange (location, 1)]; NSLog (@ "% @", result); } - (zero) processUserInput: (NSString *) string {self.string = string; // Change the string to data NSData * Data = [String Data Wrapping Encoding: NSUTF8StringEncoding]; // Pass the data to the function process (data. Bits, data, height, microback, (__breej * * *) (self)); }  

The input string only includes English characters, the code works fine. If the character sequence, multibyte character or other Unicode characters described in it, the result string in the processSymbolAtLocation method is not a required symbol

How to convert NSString objects correctly to NSData? How to get the right place?

Thank you!

Your problem is that with a UTF-16 encoded NSString Get started and create a sequence of UTF-8 encoded bytes. The number of units expected to represent the string in UTF-16 can not be equal to the number required to represent UTF-8, so offset will not match in your two forms - as you've found.

Why are you using C to scan the string of matches in the first place? You want to see the NSString range of rangeOfCharacterFromSet: options: range: method, which you can use to find the next occurrence of the character from your set.

If you need to use C, then convert your string to the sequence of UTF-16 words and use uint16_t on the C side.

HTH


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

lua - HowTo create a fuel bar -