Question

I want to know how to convert the emoji to hex value.That is for the smiley having 'SMILING FACE WITH OPEN MOUTH AND SMILING EYES' (smiley corresponds to :) ) I should get "1F604"

Was it helpful?

Solution

Is this what you are looking for?

NSString *smiley = @"😄";

NSData *data = [smiley dataUsingEncoding:NSUTF32LittleEndianStringEncoding];
uint32_t unicode;
[data getBytes:&unicode length:sizeof(unicode)];
NSLog(@"%x", unicode);
// Output: 1f604

Reverse direction:

uint32_t unicode = 0x1f604;

NSString *smiley = [[NSString alloc] initWithBytes:&unicode length:sizeof(unicode) encoding:NSUTF32LittleEndianStringEncoding];
NSLog(@"%@", smiley);
// Output: 😄

Remark: Both code examples assume that integers are stored in little-endian byte order (which is the case for all current platforms running OS X or iOS).

OTHER TIPS

Swift Version

let smiley = "😊"

let uni = smiley.unicodeScalars // Unicode scalar values of the string
let unicode = uni[uni.startIndex].value // First element as an UInt32

println(String(unicode, radix: 16, uppercase: true))
// Output: 1F60A
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top