Nsstring to Emoji Unicode

NSString to Emoji Unicode

In order to convert these unicode characters into NSString you will need to get bytes of those unicode characters.

After getting bytes, it is easy to initialize an NSString with bytes. Below code does exactly what you want. It assumes jsonArray is the NSArray generated from your json getting pulled.

// initialize using json serialization (possibly NSJSONSerialization)
NSArray *jsonArray;

[jsonArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *charCode = obj[@"unicode"];

// remove prefix 'U'
charCode = [charCode substringFromIndex:1];

unsigned unicodeInt = 0;

//convert unicode character to int
[[NSScanner scannerWithString:charCode] scanHexInt:&unicodeInt];

//convert this integer to a char array (bytes)
char chars[4];
int len = 4;

chars[0] = (unicodeInt >> 24) & (1 << 24) - 1;
chars[1] = (unicodeInt >> 16) & (1 << 16) - 1;
chars[2] = (unicodeInt >> 8) & (1 << 8) - 1;
chars[3] = unicodeInt & (1 << 8) - 1;

NSString *unicodeString = [[NSString alloc] initWithBytes:chars
length:len
encoding:NSUTF32StringEncoding];

NSLog(@"%@ - %@", obj[@"meaning"], unicodeString);
}];

Dynamically create NSString with Unicode emoji

A step back, for a second: that number that you have, 1F660316, is a Unicode code point, which, to try to put it as simply as possible, is the index of this emoji in the list of all Unicode items. That's not the same thing as the bytes that the computer actually handles, which are the "encoded value" (technically, the code units.

When you write the literal @"\U0001F603" in your code, the compiler does the encoding for you, writing the necessary bytes.* If you don't have the literal at compile time, you must do the encoding yourself. That is, you must transform the code point into a set of bytes that represent it. For example, in the UTF-16 encoding that NSString uses internally, your code point is represented by the bytes ff fe 3d d8 03 de.

You can't, at run time, modify that literal and end up with the correct bytes, because the compiler has already done its work and gone to bed.

(You can read in depth about this stuff and how it pertains to NSString in an article by Ole Begemann at objc.io.)

Fortunately, one of the available encodings, UTF-32, represents code points directly: the value of the bytes is the same as the code point's. In other words, if you assign your code point number to a 32-bit unsigned integer, you've got proper UTF-32-encoded data.

That leads us to the process you need:

// Encoded start point
uint32_t base_point_UTF32 = 0x1F600;

// Generate random point
uint32_t offset = arc4random_uniform(10);
uint32_t new_point = base_point_UTF32 + offset;

// Read the four bytes into NSString, interpreted as UTF-32LE.
// Intel machines and iOS on ARM are little endian; others byte swap/change
// encoding as necessary.
NSString * emoji = [[NSString alloc] initWithBytes:&new_point
length:4
encoding:NSUTF32LittleEndianStringEncoding];

(N.B. that this may not work as expected for an arbitrary code point; not all code points are valid.)


*Note, it does the same thing for "normal" strings like @"b", as well.

Display Unicode String as Emoji

Remove the excess backslash then convert with a reverse string transform stringByApplyingTransform. The transform must use key "Any-Hex" for emojis.

NSString *payloadString = @"\\U0001F6A3\\U0000200D\\U00002640\\U0000FE0F";
NSString *unescapedPayloadString = [payloadString stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];
NSString *transformedString = [unescapedPayloadString stringByApplyingTransform:@"Any-Hex" reverse:YES];

NSLog(@"%@", transformedString);//logs "‍♀️"

NSString with Emojis

Not my best work, but it appears to work:

for (NSString *string in array)
{
@autoreleasepool {
NSScanner *scanner = [NSScanner scannerWithString:string];
unsigned int val = 0;
(void) [scanner scanHexInt:&val];
NSString *newString = [[NSString alloc] initWithBytes:&val length:sizeof(val) encoding:NSUTF32LittleEndianStringEncoding];
NSLog(@"%@", newString);
[newString release]; // don't use if you're using ARC
}
}

Using an array of four of your sample value, I get four pairs of bare feet.

iOS 5: How to convert an Emoji to a unicode character?

\ue415 is part of the legacy encoding for emoji and is specific to certain Japanese carriers. SoftBank, NTT and docomo all had their own private emoji character sets.

iOS 5 has moved to the newly specified Unicode 6.0 support for emoji character planes and <0001f604> is the Unicode code point for that character. The wikipedia entry about this references an EmojiSources.txt mapping file that you'll need to use to do the mapping yourself if you really need to get the old private-use character codes.

http://en.wikipedia.org/wiki/Emoji



Related Topics



Leave a reply



Submit