Function HEX is one of the core functions in sqlite
I would like to call the function from my objective-c code without actually creating a database in my app .. and so on.
What would be the simplest way to call the function with the least addition of classes?
I found lots of functions written in o-c that HEX a string. However, they are not correct as they produce results different from the HEX function of mysql.
Solution: Converting a string to its Hex value
+(NSString *) stringToHex:(NSString *)str
{
NSString *aResult;
sqlite3 *database;
if(sqlite3_open(":memory:", &database) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:@"select HEX('%@')",str];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
aResult = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return aResult;
}
You can open an in-memory db using sqlite3_open and use that. To do this, open a db with the file name
:memory:(w/ the colons).If you only want the hex function, you can also look into the source code of SQLite. It’s plain C and in the public domain.