Difficulty dealing with encoding in SQLite - ios

I'm trying to get information from a SQLite file, and I when I run a query, the information is returned with ASCII encoding. I'm using the code below to put the returned information into a string.
[NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 2) encoding:NSASCIIStringEncoding];
When I try to use UTF8 encoding to put the returned information into a string, it doesn't work. The code below is used in the app currently on the store.
[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
The string is null. How can I convert the SQLite file so that it is encoded with UTF8? The current version of the app on the store uses the latter code above, so I need to figure out how to convert the file, instead of changing the code in the app.

You can use const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); with stringWithCharacters:length: (UTF16)
const unichar *text = sqlite3_column_text16(_sqlite_stmt, columnIndex);
if (text) {
NSUInteger length = sqlite3_column_bytes16(_sqlite_stmt, columnIndex)/sizeof(unichar);
article = [NSString stringWithCharacters:text length:length];
}

Try this :
char *title = (char *)sqlite3_column_text(compiledStatement, 2);
NSString *str = [NSString stringWithUTF8String:title];
Working fine for me !

Related

Display Unicode String as Emoji

I currently receive emojis in a payload in the following format:
\\U0001F6A3\\U0000200D\\U00002640\\U0000FE0F
which represents "🚣‍♀️"
However, if I try to display this, it only shows the string above (escaped with 1 less ), not the emoji e.g.
NSString *emoji = payload[#"emoji"];
NSLog(#"%#", emoji) then displays as \U0001F6A3\U0000200D\U00002640\U0000FE0F
It's as if the unicode escape it not being recognised. How can I get the string above to show as an emoji?
Please assume that the format the data is received in from the server cannot be changed.
UPDATE
I found another way to do it, but I think the answer by Albert posted below is better. I am only posting this for completeness and reference:
NSArray *emojiArray = [unicodeString componentsSeparatedByString:#"\\U"];
NSString *transformedString = #"";
for (NSInteger i = 0; i < [emojiArray count]; i++) {
NSString *code = emojiArray[i];
if ([code length] == 0) continue;
NSScanner *hexScan = [NSScanner scannerWithString:code];
unsigned int hexNum;
[hexScan scanHexInt:&hexNum];
UTF32Char inputChar = hexNum;
NSString *res = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
transformedString = [transformedString stringByAppendingString:res];
}
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 "🚣‍♀️"
I investigated this, and it seems you may not be receiving what you say you are receiving. If you see \U0001F6A3\U0000200D\U00002640\U0000FE0F in your NSLog, chances are you are actually receiving \\U0001F6A3\\U0000200D\\U00002640\\U0000FE0F at your end instead. I tried using a variable
NSString *toDecode = #"\U0001F6A3\U0000200D\U00002640\U0000FE0F";
self.tv.text = toDecode;
And in textview it is displaying the emoji fine.
So you got to fix that first and then it will display well.

Get unicode chars from webservice and display them in ios app

I need some help:
i get from WebService only a part from the uunicode value, and after this I append the prefix \u to finish the value. The .ttf is good, i tested with some hardcoded values.
NSString *cuvant = [[self.catData objectAtIndex:indexPath.row]objectAtIndex:9]; //Get data
//apend prefix (double \ to escape the \u command)
cuvant = [NSString stringWithFormat:#"\\u%#",cuvant];
// cell.catChar.text = [NSString stringWithUTF8String:"\ue674"]; --->this works very well
cell.catChar.text = [NSString stringWithUTF8String:[cuvant UTF8String]]; //---> this doesn't work
i searched the documentation, and other sites but i didn't found nothing usefull, all the hints are with hardcoded data... i need tot take the codes dinamically
Thanks!
All you need is just to feed this unicoded string as data first. So make a C-String then
NSData *dataFromUnicodedString = [NSData dataWithBytes:yourCUnicodedString length:strlen(yourCUnicodedString)];
and afterwards the resulting string will be
NSString *unicodedString = [[NSString alloc] initWithData:dataFromUnicodedString encoding:NSUTF8StringEncoding];

How to encode the string in to "windows-1252" using ios?

The following string is working perfectly in android,please give me suggestion for encoding this in ios.
Android Example:String s = "hhh";
s.getBytes("Windows-1252");
A quick look at the docs for NSString would give you:
NSString *s = #"hhh";
NSData *data = [s dataUsingEncoding:NSWindowsCP1252StringEncoding];
uint_8 *bytes = [data bytes];
The equivalent code in iOS looks like this
NSString *str = #"hhh";
char buffer[100];
[str getCString:buffer maxLength:100 encoding:NSWindowsCP1252StringEncoding];

Replace a Portion of NSMutableData with a NSString

I have some NSMutableData holding some ASCII code.
I would like to replace some bytes in a specific range with other bytes made up of an NSString:
NSString *myString = #"\r"
const char *myChar = [myString UTF8String];
[myData replaceBytesInRange:myRange withBytes:&myChar];
Now, when logging myData I get different values for the exchanged byte every time I run the App! Aren't I supposed to get <0D>?!
What am I doing wrong?

sha-512 not working properly on IOS Simulator

I am implementing mobile app sha-512 function that will run on IOS, this function is working fine on all browsers including Safari.
I am testing my application using IOS Simulator.
My application is calling sha-512 function three times from one page/click. The problem is that, at first call sha-512 function produces right result, but at second and third call it produces wrong result.
thanks in advance
This is my code
//Creating Hash Value
NSString *hashkey = [NSString stringWithFormat:#"data"];
// PHP uses ASCII encoding, not UTF
NSLog(#"hashkey : %#",hashkey);
const char *s = [hashkey cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
// This is the destination SHA512_Final
uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA512(keyData.bytes, keyData.length, digest);
// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:#" " withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#"<" withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#">" withString:#""];
Hope it helps you..

Resources