Proper way to get the byte array from the JSON - ios

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?

Related

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

Change UIImage Class to send to node.js server in iOS?

I am creating an App for chatting. Now I want to send UIImage to server in JSON String so other user can receive image.I am using socket.io so I have to send event with data(JSON String).
Problems- When I try to convert UIImage to NSData and convert it to JSON it gives error 'Invalid type in JSON write (NSConcreteMutableData)'.
What will be the correct way to send UIImage to server?
code
NSData *imgData = UIImageJPEGRepresentation(image, .2);
NSString * imageString =[[NSString alloc]initWithBytes:[imageData bytes] length:[imageData length] encoding:NSASCIIStringEncoding];
also tried
NSString * imageString =[[NSString alloc]initWithData :imgData encoding:NSUTF8StringEncoding];
converted data to dictionary:
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:imgData options:NSJSONReadingAllowFragments error:&error];
Any help would be appreciated.
The general approach would be to base64 encode the image data. In this way the image data is converted to a string format instead of binary.
Since iOS 7 NSData has provided base64 conversion methods that you can use instead of your current attempted conversion to and from strings.

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

Base64 encoded string wont decode (iOS)

I am base64 encoding some data using the following line:
NSString *theData = [serialized base64EncodedStringWithOptions:kNilOptions];
This works correctly and I then pass this string to my web server which stores it in the database.
Later, I am then retrieving this base64 encoded string back from the web server which also works correctly (I have compared both the original before upload and the after download strings and they are the same).
However, when I try to decode this string using:
NSString *theString = [imageDict objectForKey:#"image"];
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:theString
options:kNilOptions];
it just gives me a null value for imageData.
If I output theString it is the correct base64 encoded string I uploaded.
Any ideas why it won't decode?
Thanks in advance.
I used NSDataAdditions.h < see http://code.ohloh.net/file?fid=28qaXmo6xH1Z4clfmn9_wJqDqNI&cid=xVjpNPxNo_A&s=&fp=308694&mp=&projSelected=true#L0 and for .m http://code.ohloh.net/file?fid=tXQCCVHemN1iAx6ZQSy1VkBACXA&cid=xVjpNPxNo_A&s=&fp=308694&mp&projSelected=true#L0 >
NSData *imageData = [NSData dataWithBase64EncodedString:theString];

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.

Resources