Converting sqlite3_column_blob to NSString - ios

I have been trying to add sqlite3_column_blob type of value to NSString by this lines of code :
NSUInteger len = sqlite3_column_bytes(compiledStatement, n);
NSData *cData = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, n) length:len];
NSString *columnText = [NSString stringWithUTF8String:[cData bytes]];
Here is the BLOB data which i am trying to retrieve from the database :
�PNG
IHDRXXq�04sBIT|d�
�IDATx��{pEǿ� !�f��E���0��#����Q'p#iQ��È�L�P���R`6 F�COJD�q��0 ��\�FbrȁR�������c6��������f�����M��-����n"�~;�p0s��bɶX,c�U^7!�Q!��e�n�2��o�%��#���~3G!�^�X,��<�b��B\S�)ˬ`��Ť*S��#�.�9+c�8ADtN�fd�}����D4��̊�X����u9��f�y̜/_�c�
"J�D�V0�^"�0��h6��Du��,3s83���f"z��j���6M]!V�[q���\��+��O�QUU�K�.*~:���#HH�|�]�F�#tt�'�C�$wU�+<ט�� >}���`��rR�b<�sv��\��'Nb�8~�$��F��h����<z$��1�U{�J�t#'9��~e��X|*�b�I��]��+�:|F��B��6$IB��~xd��>aa����$�/�j��f�H��
rV����ز{>��ju1�Q��'�E�+}Q�/^��&����r�۰Ǿ>����A�:g�S��(��r}Q�Of���
��t��omƶw5Y7�-�$aꔉH�5]��>KD�}1�J`f0#7!DM`7u=p�0Vg��ҥ_��&%,�2^L�#Æ��ʘ�CI�*�8HD���-03�oko��7n"7�mߥ�ѷ���'b��9v:r='���z�������V^ͅ_���E�%z����bU�rt���|"z����y���ݴ��+0��y+.�`��P^^ᬈ�ow�n����S�� %5���[O�t�W������3yB�:]3s["�L;u��g4z�O��uH[�� 2=.�=ƣ>��;x��gA1+/�#JjZ�W����u&DG�����l%�jw�p)03��h�:���/�����^���"<�nl|#O����[$I��jdᮋ��%�7����ŋ���4�eܸq�!O�f���ݙ+;h%��o�z�зOO�I�1����`��g��*�yȫg���pd�x�v]__�=��Ǟ��4?^�()-Gn�,yA�(���wo��A���G��D��Sv&&No'.,9�����J����/�ۥL�0��L����s�m߅ke�4ڻ��.j6�aUV�ΦݢW�D�V!$$D3?$$��L�̞�j6���6H�=��L�u�Vy-r֚��z�7u�K۱K�����u6z���Ꙉ��?�Jpp̚��{�M����Xn�hŸ��x�71k�Sn�8!!!����M����
oۥ�5R�9�`�NB��D4˚VRZ��3��m����<���Ǿ>�s�.�����g#d���о���A��ᴨ�����t������
$I���ۙ:��-I�^$"������<�����_X�G��jL���Vy%ns��C�$!絕��+D�X�Zm�E0sof���E�%-N\���/ߴ
=t�w�h��(�3odf;+���Tc���j#���������Sv�n�]hb����nVRUU�lx�I��x��3=�#�Fc(Lٙ���=��[�N��-
m� ,O��V$?��غ'�#oͪ�׊���5�<Y�O?s0
�WO��i�l���
p|\l��k�*r|\�G����v�j�Ǐ���l~
ߝ�5~q[Q|\,^�{-�ŵb4�����<����nBD�d�6������h��9���J��b�o
�7W����8���b�}�y�,03G1s!T.���O�|�U�����h~``4�"�侻(<~R�4���9
�B,`�T__���2�n���.k��Zi߾V������攨 R���E8�7~8W�r�p��M,{e�W�[?�Ѭ� fs�~�ë����R�l�����L���$i=T�_+�6��K0w�R]bm�`rr�y\�W���%��\���zd����$IkY`"�ff�9�?���!zD޶}�m�JN�:�l�z��b����:�(B����.x0<�RT\���]��m�.d����MEV�Z�"��uX�(]׊�Z+I����_�n�TQS����>ۙ��w~��Z��Y��;?qH���ù"]����fذ�"�9SY��o�{�h��}�'X��OP��:+�Ndo��b�L�^=Iv=}#��еk�|�l(E�Wq����Ů�v7J\#S�pf~���3?�̛�%���ѫʬ<y?��+Z���Q�����:�3�i���zB�/҈h���̊7�J��� �Z/� �:'�������)�u"z����Z����rЪ���$��։F%e(K�� �XSӾ��=��2�����`;��=�w7}�Z�Zٴ� ,��sb�׉}+�t����Z��u�AD)�B�{�[q�Z+"J�ƫ�ڃ����V9��
��
!�~�":�Kx�k��'8��3����`I�V�ۛk0�{S�G���Z�q���6{p%8�,�4�o�42���i>IDC��P�"����FC1���^ڊ���(/ˈh�rs�ڳ���[�{$&�kW���t�a��2��DtEY��+FDq��?�m�U�%��F����c���06ƍMnM8a�X��Q��6��������}�1��־X�����>�ѭ�(j�5m�3�;l���q��s�J"���R��9��C�|��#f�Z.Uf"J%"���#D�(���U��I�����&��9��Ҙ�]"�!���BD5AAAO3�m�=6&�&��*~G1i�x���AAAO����f��$I�)����4�X�$,��̴KSk��ݿy;g����a�"�A�Z<K���&u2ҝ���Q����u6-�<����p�,�����V���s�E�kQ��Dc�¹β55��I#�MD�:���)��ZD�W��uB��5�6f�*I�W9<
)`3?�c�R�RfK�Cʨaf�����盓���8åx ���L���,%� N�k�#D�̼[�ֿ_�Yݢ<��֬v0�3�n���tO�$Ir�ڷOO�7��1rXX'�7�o��yZ����}5���Dc�y=���78�v�o�����c���������A��}�Up�b"����A�a~�(!D=���_��\\���a�m��:��%�&̼S��b��]�f#3��Jd��
6���<u�D�I���^&�����b\94L�/�j� �E�%شy���d�a�1}�����$�����ڋ8p�0�-l�c�I���ô�L���c�A%����tE1���wv�m>�d�|P�5T-��~{P�3,�j�X-�����QY�3��+PU]�˗�#JJ�����DC��бct��#��(DFޏ޽=Z�b悠���:j�r��R��aQ1Ζy|I��b�~o/)���g�h�R�!r��XN'���C\����DtV��M>����Z�9�G����L!D�Ƒ�
�%S.��GN�
�CSW4����vhj�v�!P���?� �kɲIEND�B`�
But every time i am getting nil response although there is a value exist. Rest of the parameter values are coming just i am getting nil response in above value.
Thanks.

The reason you cannot convert that data to a string is because it's a PNG image file:
�PNG
(I assume; I haven't looked at the PNG spec to confirm this, it's just an assumption at this stage).

Finally i got the solution for the above issue which i was facing :
NSData *content = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 2) length:sqlite3_column_bytes(compiledStatement, 2)];
NSString *columnText = [content base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

Related

How to convert Base64 encoded NSString into Byte Array(Java) in ios?

I have an Image that is converted into NSData and which in-turn is converted into base64 encoded NSString. Now i have a service that accepts this base64 encoded string in only Byte Array(Java Supported). I tried different options but i am not able to convert the encoded string into Byte Array type. Can someone please help me on how to convert the encoded string into Byte Array(Java supported)?
Below is my answer for this. I came to know that the easiest way is to convert the image data into byte array directly without base64encoding the data.
NSError* error;
NSString* str=[[NSBundle mainBundle] pathForResource:#"black-circle" ofType:#"png"];
NSData* data= [NSData dataWithContentsOfFile:str options:NSDataReadingUncached error:&error];
NSInteger length=[data length];
const char* dataBytes=[data bytes];
Now you can traverse each byte using dataBytes pointer.
Hope this helps.
You can try out :
Use-[NSString UTF8String] which returns const char*.
or this link will help you :
Convert NSString into byte array iphone
We can get the NSdata from the NSStirng and then convert NSData into Byte Array. The easier way is to convert the captured image data into Byte Array using uint8_t. Below is the code snippet that does the trick.
NSData to Byte Array:-
NSData *data = UIImagePNGRepresentation(imageCaptured);
uint8_t byteArray[[data length]];
[data getBytes:&byteArray];
NSMutableString *byteArrayString = [[NSMutableString alloc]init];
for (int i = 0 ; i < sizeof(byteArray); i++) {
[byteArrayString appendString:[NSString stringWithFormat:#"%d,",byteArray[i]]];
}
// Removing last extra comma
[byteArrayString deleteCharactersInRange:NSMakeRange([byteArrayString length]-1, 1)];

Proper way to get the byte array from the JSON

I am trying to get the image from the byte array. I can only get the image if enter the byte array values into the string directly as follows:
NSMutableString *imagen = [[NSMutableString alloc] initWithString:#"-1,-40,-1,-32,0,16,74,70,73,70,0,1,0,1,0,96,0,96,0,0,-1,-2,0,31,76,69,65,68,32,84,101,99,104,110,111,108,111,103,105,101,115,32,73,110,99,46,32,86,49,46,48,49,0,-1,-37,0,-124,0,5,5,5,8,5,8,12,7,7,12,12,9,9,9,12,13,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13];
//this way works fine
IF i try to get the byte array into string as follows,then I couldnt get the image:
NSString *logo=[NSString stringWithFormat:#"%#",[[JSON valueForKey:#"request"]valueForKey:#"logo" ]];
NSMutableString *imagen = [[NSMutableString alloc] initWithString:logo];
//this way doesnt work out
I am following this link to get the thing done.this link
Could you please tell me whats the proper way to get byte array from JSON?
This is not executable code, it is a method to use:
You get the JSON in self.receivedData
Convert it into an object with
NSDictionary *jsonObject = NSJSONSerialization JSONObjectWithData:
** Unknown how the image data is encoded in the JSON. If it is Base64 encoded:
Get the Base64image string with
NSString *imageString = jsonObject[#"request"][#"logo"]
Convert the Base64 string into data:
NSData *imageData = [NSData alloc] initWithBase64EncodedString: imageString options:
Get an image with
UIImage *logoImage = [imageData imageWithData]
All in all you have way to much code that accomplished nothing and converting the image data to an NSString is incorrect.
Converting to a string and then back to data accomplishes nothing.
This code seems confused? The link you posted has string encodings which contain negative numbers. You parse these as signed but then assign them to an array of unsigned uint8_t using bytes[i] = (uint8_t)byte;.
I think you need to post the string encoding format and an example?

NSString from NSData is nil

I try to get an NSString from NSData but I get a nil value.
This is my code:
NSString *dataString = [[NSString alloc] initWithData:self.message.data encoding:NSUTF8StringEncoding];
When I log self.message.data I get:
Printing description of self->_message->_data:
<OS_dispatch_data: data[0x17e48290] = { leaf, size = 331233, buf = 0x3aac000 }>
That means my data is not nil…
Can anyone help?
As answer to your question in comments:
OS_dispatch_data is dispatch_data_t which has toll-free bridging with NSData on iOS 7 and Mavericks. You can simply cast it to NSData *.
So, in your case you can write:
NSData *dataCast = self.message.data;
NSString *dataString = [[NSString alloc] initWithData:dataCast encoding:NSUTF8StringEncoding];
And now you get the correct string!
In my case it appeared, when AFNetworking cast internal NSMutableData to NSData.
And this simple cast helps me.
UPD: As #Daij-Djan mentioned: If it's not works - try to check your text encoding.
For example if you're yousing NSURLSessionTask:
NSURLSessionTask *task; // Your NSURLSessionTask
NSString *encoding = [[task response] textEncodingName];
In your case (NSUTF8StringEncoding) it should be "utf-8".
Try UTF-16 encoding instead. It may be an error converting to UTF-8 if any data in there is not recognized.

Encoding for converting between NSString to NSData and back

I'm trying to encrypt/decrypt an NSString and return the original string in the end. Here's how I convert the string to a data object:
NSData *string_data = [string dataUsingEncoding:NSUTF8StringEncoding];
And after that data has been encrypted/decrypted I want it back to the original string by doing:
NSString *to_string = [NSString stringWithCString:[decrypted_data bytes] encoding:NSUTF8StringEncoding];
The encoding seems to match, but I still get a null when I try to print out to_string to the console. I've tried all sorts of encoding settings. It doesn't seem to work.
Use:
NSString *to_string = [[NSString alloc] initWithData:string_data encoding:NSUTF8StringEncoding];
It is not safe to use stringWithCString because the bytes buffer you get from NSData is not guaranteed to be null-terminated.

How to convert NData populated with hex values to NSString

I have a NSdata object that is populated with a bunch of information thats formated in hex.. I am trying to convert it into its proper string representation but am struggling to have any success.
One thing I have tried is to simply put it into a NSString and then NSLog it with a special character identifier thingy.. forgot the word (%02x), However to do this I am encoding it to NSUTF16.. which i dont want to do.. I mearly want to see exactly whats the data I am getting looks like as a NSString.
The reason I am doing this is because I am having some issues with my encoding later on in my code and im not sure if its because the data I am receiving is incorrect or me stuffing it up at some point when I am handling it.
Any help would be appreciated.
You can get a string representation of your NSData like so:
NSData *data = (your data)
NSString *string = [NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding];
Does that answer your question?
Maybe I haven't understood, but something like this:
NSData *yourData;
NSLog(#"%#", [yourData description]);
doesn't fit your need?
Give this a try -
-(NSString*)hexToString:(NSData*)data{
NSString *hexString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
if (([hexString length] % 2) != 0)
return nil;
NSMutableString *string = [NSMutableString string];
for (NSInteger i = 0; i < [hexString length]; i += 2) {
NSString *hex = [hexString substringWithRange:NSMakeRange(i, 2)];
NSInteger decimalValue = 0;
sscanf([hex UTF8String], "%x", &decimalValue);
[string appendFormat:#"%d", decimalValue];
}
return string;
}

Resources