3
votes

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"

2

2 Answers

5
votes

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).

1
votes

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