NSString to Emoji Unicode - ios

I am trying to pull an JSON file from the backend containing unicodes for emoji. These are not the legacy unicodes (example: \ue415), but rather unicodes that work cross platform (example: \U0001F604).
Here is a sample piece of the json getting pulled:
[
{
"unicode": "U0001F601",
"meaning": "Argh!"
},
{
"unicode": "U0001F602",
"meaning": "Laughing so hard"
}
]
I am having difficulty converting these strings into unicodes that will display as emoji within the app.
Any help is greatly appreciated!

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);
}];

Related

Converting very large NSDecimal to string eg. 400,000,000,000 -> 400 T and so forth

I am making a game that requires me to use very large numbers. I believe I am able to store very large numbers with NSDecimal. However, when displaying the numbers to users I would like to be able to convert the large number to a succinct string that uses characters to signify the value eg. 100,000 -> 100k 1,000,000 -> 1.00M 4,200,000,000 -> 4.20B and so forth going up to extremely large numbers. Is there any built in method for doing so or would I have to use a bunch of
NSDecimalCompare statements to determine the size of the number and convert?
I am hoping to use objective c for the application.
I know that I can use NSString *string = NSDecimalString(&NSDecimal, _usLocale); to convert to a string could I then do some type of comparison on this string to get the result I'm looking for?
Use this method to convert your number into a smaller format just as you need:
-(NSString*) suffixNumber:(NSNumber*)number
{
if (!number)
return #"";
long long num = [number longLongValue];
int s = ( (num < 0) ? -1 : (num > 0) ? 1 : 0 );
NSString* sign = (s == -1 ? #"-" : #"" );
num = llabs(num);
if (num < 1000)
return [NSString stringWithFormat:#"%#%lld",sign,num];
int exp = (int) (log(num) / 3.f); //log(1000));
NSArray* units = #[#"K",#"M",#"G",#"T",#"P",#"E"];
return [NSString stringWithFormat:#"%#%.1f%#",sign, (num / pow(1000, exp)), [units objectAtIndex:(exp-1)]];
}
Some sample examples:
NSLog(#"%#",[self suffixNumber:#99999]); // 100.0K
NSLog(#"%#",[self suffixNumber:#5109999]); // 5.1M
Source
Solved my issue: Can only be used if you know that your NSDecimal that you are trying to format will only be a whole number without decimals so make sure you round when doing any math on the NSDecimals.
-(NSString *)returnFormattedString:(NSDecimal)nsDecimalToFormat{
NSMutableArray *formatArray = [NSMutableArray arrayWithObjects:#"%.2f",#"%.1f",#"%.0f",nil];
NSMutableArray *suffixes = [NSMutableArray arrayWithObjects:#"k",#"M",#"B",#"T",#"Qa",#"Qi",#"Sx",#"Sp",#"Oc",#"No",#"De",#"Ud",#"Dud",#"Tde",#"Qde",#"Qid",#"Sxd",#"Spd",#"Ocd",#"Nvd",#"Vi",#"Uvi",#"Dvi",#"Tvi", nil];
int dick = [suffixes count];
NSLog(#"count %i",dick);
NSString *string = NSDecimalString(&nsDecimalToFormat, _usLocale);
NSString *formatedString;
NSUInteger characterCount = [string length];
if (characterCount > 3) {
NSString *trimmedString=[string substringToIndex:3];
float a;
a = 100.00/(pow(10, (characterCount - 4)%3));
int remainder = (characterCount-4)%3;
int suffixIndex = (characterCount + 3 - 1)/3 - 2;
NSLog(#"%i",suffixIndex);
if(suffixIndex < [suffixes count]){
NSString *formatSpecifier = [formatArray[remainder] stringByAppendingString:suffixes[suffixIndex]];
formatedString= [NSString stringWithFormat:formatSpecifier, [trimmedString floatValue] / a];
}
else {
formatedString = #"too Big";
}
}
else{
formatedString = string;
}
return formatedString;
}

How to read binary data stored in NSData

I am getting data from a web call. Here is data.
00000001 00045043 4c4e0000 00023744 92f4cd44 92f4cd44 92f4cd44 92f4cd3c a3d70a00 00014de2 b767a044 93800044 93800044 93800044 93800040 a0000000 00014de2 bdd04044 94400044 94400044 93db3344 94170a3f a6666600 00014de2 c8ccc044 93ddc344 93ddc344 93ddc344 93ddc33e 99999a00 00014de2 ce4b0044 93bd1f44 943d7144 9394cd44 93eccd41 e8e14800 00014de2 d01fc044 93eb3344 93eb3344 93b00044 93b00040 c0000000
The data format is fixed (We know where it stores int / float etc.
Question is how do I read and store it in individual variables
For example the first 4 bytes here are specifying the number of symbols returned (1)
NSMutableString *stringHexForm = [NSMutableString string];
for(int i = startLocation ; i < (startLocation + nCharsToReadIn) ; i++)
{
unsigned char byte;
[dataIn getBytes : &byte range : NSMakeRange(i, 1)];
NSString *tempStr=[NSString stringWithFormat:#"%02x",byte];
[stringHexForm appendString:[NSString stringWithFormat:#"%#",tempStr]];
}
unsigned result = 0;
NSScanner *scanner = [NSScanner stringHexForm];
[scanner scanHexInt:&result];
return result;
NSData has the method func getBytes(buffer: UnsafeMutablePointer<Void>, range: NSRange) that should help you. You can grab each thing in your struct by specifying the range.
So you can do:
var length: Int = 0
myData.getBytes(&length, range: NSMakeRange(0, 4))
and so on for each type, range that you need

decrypt value from blowfish in Objective-C code

I am recieving the Encrypted data by server (BLOWFISH ALGORITHM) , I have to decrypt it by using blowfish algorithm in IOS.
you can donwload my code from here : https://www.dropbox.com/s/nswsm7des7isgd5/BlowfishTest-4.zip
I am struggling from 2 days with this task , I try lot of links and find few useful :
Blowfish Source code
How to implement Blowfish algorithm in iOS
http://www.codeding.com/articles/blowfish-encryption-algorithm-for-iphone
In third link, i got ECB ( I have to decrypt using ECB). but this code also not gives correct output after decryption.
I am using a online tool for testing and this shows correct output : http://www.tools4noobs.com/online_tools/decrypt/
Key = 20zE1E47BE57$51
Input value is = aed5c110d793f850521a4dd3a56a70d9
Algorithm = BLOWFISH
Mode = ECB
Decode the input using= Hexa
output = aYzY1380188405 ( this is correct output which i want)
and I am getting : ¹àÀhÒ¢º¹iÂF
Here is my code :
//Mode selected by default in nib: “ECB”
NSString *modeString = [encryptionModeControl titleForSegmentAtIndex:encryptionModeControl.selectedSegmentIndex];
BlowfishAlgorithm *blowFish = [BlowfishAlgorithm new];
[blowFish setMode:[BlowfishAlgorithm buildModeEnum:modeString]];
[blowFish setKey:key];
[blowFish setInitVector:initVector];
[blowFish setupKey];
NSString *cipherText = cipherTextView.text;
NSString *plainText = [blowFish decrypt:cipherText];
NSLog(#"cipher-text: %#", cipherText);
NSLog(#"plain-text: %#", plainText);
Note : Server side data is Encrypted using BLOWFISH in ECB mode, and converted to hexadecimal notation.
1) Source of Blowfish routines from David Madore: ftp://quatramaran.ens.fr/pub/madore/misc/blowfish.c
Pls note that in this source .h part should be separated from the .c file.
2) To use Pandora API we have to use the passwords given by its wiki page here:
http://pan-do-ra-api.wikia.com/wiki/Json/5/partners
Currently decrypt password is: 20zE1E47BE57$51
3) Use this code snippet (standing on great programmers' shoulders) - original Pandora API implementation is here: https://github.com/alexcrichton/hermes
In AppDelegate.h (for simplicity)
#define PARTNER_DECRYPT "20zE1E47BE57$51"
...
-(NSData*) PandoraDecrypt:(NSString*) string;
In AppDelegate.m
static char h2i[256] = {
['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4, ['5'] = 5, ['6'] = 6,
['7'] = 7, ['8'] = 8, ['9'] = 9, ['a'] = 10, ['b'] = 11, ['c'] = 12,
['d'] = 13, ['e'] = 14, ['f'] = 15
};
static void appendByte(unsigned char byte, void *_data) {
NSMutableData *data = (__bridge NSMutableData*) _data;
NSLog(#"pre: %#", data);
[data appendBytes:&byte length:1];
NSLog(#"post: %#", data);
}
-(NSData*) PandoraDecrypt:(NSString*) string {
struct blf_ecb_ctx ctx;
NSMutableData *mut = [[NSMutableData alloc] init];
Blowfish_ecb_start(&ctx, FALSE, (unsigned char*) PARTNER_DECRYPT,
sizeof(PARTNER_DECRYPT) - 1, appendByte,
(__bridge void*) mut);
const char *bytes = [string cStringUsingEncoding:NSASCIIStringEncoding];
int len = [string lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
int i;
for (i = 0; i < len; i += 2) {
NSLog(#"%c, %c, %d, %d", bytes[i], bytes[i+1], h2i[(int) bytes[i]] * 16, h2i[(int) bytes[i + 1]]);
Blowfish_ecb_feed(&ctx, h2i[(int) bytes[i]] * 16 + h2i[(int) bytes[i + 1]]);
}
Blowfish_ecb_stop(&ctx);
return mut;
}
And you can use this like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(#"%#", [NSString stringWithCString:[
[self PandoraDecrypt:#"aed5c110d793f850521a4dd3a56a70d9"] bytes]
encoding:NSASCIIStringEncoding]);
return YES;
}
So it was mainly a research from my side, pls give credit to implementers of the Blowfish api and the pandora api ;-)
Also my NSLogs are for research purpose, it highlights how the decryption works.

Chinese character to ASCII or Hexadecimal

Im struggling to covert chinese word/characters to ascii or hexadecimal and all the values I've got up until now is not what I was suppose to get.
Example of conversion is the word 手 to hex is 1534b.
Methods Ive followed till now are as below, and I got varieties of results but the one I was looking for,
I really appreciate if you can help me out on this issue,
Thanks,
Mike
- (NSString *) stringToHex:(NSString *)str{
NSUInteger len = [str length];
unichar *chars = malloc(len * sizeof(unichar));
[str getCharacters:chars];
NSMutableString *hexString = [[NSMutableString alloc] init];
for(NSUInteger i = 0; i < len; i++ )
{
[hexString appendFormat:#"%02x", chars[i]]; //EDITED PER COMMENT BELOW
}
free(chars);
return hexString;}
and
const char *cString = [#"手" cStringUsingEncoding:NSASCIIStringEncoding];
below is the similar code in Java for Android, Maybe it helps
public boolean sendText(INotifiableManager manager, String text) {
final int codeOffset = 0xf100;
for (char c : text.toCharArray()) {
int code = (int)c+codeOffset;
if (! mConnection.getBoolean(manager, "SendKey", Integer.toString(code))) {
}
Your Java code is just doing this:
Take each 16-bit character of the string and add 0xf100 to it.
If you do the same thing in your above Objective-C code you will get the result you want.

How do I compare characters in custom sqlite collation in objective-c?

I went through lots of questions here on SO (like this one) but I still need some assistance.
I need my sqlite select to order by slovenian alphabet (letter č comes after c, letter š after s and letter ž after z).
Here is the code I use:
static int sqlite3SloCollate(void * foo, int ll, const void *l, int rl,
const void *r){
NSString *left = [NSString stringWithCharacters:l length:ll];
NSString *right = [NSString stringWithCharacters:r length:rl];
//THIS IS WHERE I DON'T KNOW HOW TO COMPARE CHARACTERS
NSComparisonResult rs = [left compare:right options:NSForcedOrderingSearch];
return rs;
}
sqlite3_create_collation(database, "SLOCOLLATE", SQLITE_UTF8, NULL, &sqlite3SloCollate);
querySQL = [NSString stringWithFormat: #"SELECT s.id FROM lyrics l INNER JOIN song s ON (l.idSong=s.id) WHERE content LIKE '%%%#%%' GROUP BY s.id ORDER BY s.title COLLATE SLOCOLLATE;",searchString];
Which NSOrdering type should I use? Or do I have to write my own compare function (can you give me an example)?
I think that this function might help you :
- (NSComparisonResult)compare:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)range locale:(id)locale
(From Apple documentation).
You can create a locale using :
- (id)initWithLocaleIdentifier:(NSString *)string
(From Apple NSLocale Class Documentation).
This code should do the trick :
NSRange range = NSMakeRange(0, [left length]);
id locale = [[NSLocale alloc] initWithLocaleIdentifier:#"sl_SI"];
NSComparisonResult rs = [left compare:right options:NSForcedOrderingSearch range:range locale:locale];
I hope this will help.
The #DCMaxxx answer has most of it. Plus the comment that you need to use stringWithUTF8String. But there's some more issues.
1) stringWithUTF8String uses null-terminated c-strings, whilst sqlite is suppling strings with just a length and no null termination.
2) For the number of characters to compare, we need to take the shortest length, not just the left length.
3) When the comparison is equal for the compare, we then need to consider which string is longer.
Full code here. I use an NSMutableData object to convert length coded strings to null terminated strings. It's probably quicker and easier to do it with straight c code, if you are that way inclined.
static int sqlite3SloCollate(void * foo, int ll, const void *l, int rl,
const void *r){
NSMutableData* ld = [NSMutableData dataWithBytes:l length:ll+1];
[ld resetBytesInRange:NSMakeRange(ll, 1)];
NSString *left = [NSString stringWithUTF8String:[ld bytes]];
NSMutableData* rd = [NSMutableData dataWithBytes:r length:rl+1];
[rd resetBytesInRange:NSMakeRange(rl, 1)];
NSString *right = [NSString stringWithUTF8String:[rd bytes]];
NSRange range = NSMakeRange(0, MIN([left length],[right length]));
id locale = [[NSLocale alloc] initWithLocaleIdentifier:#"sl_SI"];
NSComparisonResult result = [left compare:right options:0 range:range locale:locale];
if (result==NSOrderedSame) {
if (ll>rl) {
result = NSOrderedDescending;
} else if (ll<rl) {
result = NSOrderedAscending;
}
}
// NSLog(#"Comparison:%# - %# - %li",left,right,(long)result);
return result;
}

Resources