3

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"

Paige DePol
  • 1,121
  • 1
  • 9
  • 23
Jeff
  • 1,405
  • 3
  • 26
  • 46

2 Answers2

5

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

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • @Martin.Thank you for your answer.I have one more doubt,how to achieve the reverse ie from '1f604' to the smiley – Jeff Jan 29 '14 at 13:05
  • Thank you so much.It works for initial set of smiley in iOS emoji key board.But for the last set i.e. starting from 1,2…# with blue border,it gives the hex value as '31' for emoji '1' and after converting to emoji I am getting digit '1' Not smiley – Jeff Jan 29 '14 at 13:17
  • 1
    @Jeff: "1⃣" is not a single character, but two characters: "1" followed by U+20E3 = COMBINING ENCLOSING KEYCAP ... – Martin R Jan 29 '14 at 13:47
  • For the last set of emojis,after the conversion of uint32_t to smiley,Almost all characters are not as expected.Any help will be appreciable ?? – Jeff Jan 29 '14 at 13:49
  • 1
    How i can convert NSString *smiley = @"This is a happy "; ? – San007 May 20 '14 at 14:26
1

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
Himanshu Parashar
  • 478
  • 1
  • 6
  • 18