Conversion of NSData to NSString? - objective-c

I am converting NSData to NSString with following code
NSData *videodata=-----?
NSString *urlstring = [[NSString alloc] initWithData:videodata encoding:NSUTF8StringEncoding] ;
I also tried
NSString *urlstring= [NSString stringWithUTF8String:[videodata bytes]];
But both returns null value.Is there any other process.Thanks

NSData *somedata = [NSData data];
NSString *str = [[NSString alloc] initWithData:somedata encoding:NSUTF8StringEncoding];
Or replace the encoding with the encoding of the data

Related

Convert UTF8String to NSString in Objective C

I want to convert UTF8String i.e.0x000060000292d290 to NSString i.e. Hx5oE/NUb7kyUfofpeQwIr1LYcRUWQGOGnsgJqSEREI=
In Objective C,
If I type po [#"Hx5oE/NUb7kyUfofpeQwIr1LYcRUWQGOGnsgJqSEREI=" UTF8String] on console then it gives me "0x000060000292d290".
How should I get reverse result?
NSData *data = [[NSData dataWithBytes:#"0x000060000292d290" length:32] base64EncodedDataWithOptions:(NSDataBase64Encoding64CharacterLineLength)];
NSString *numstr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"nonsens:%#",num);
:)

NSData to NSString conversion return nil

I get data from a server with
NSData *data=[NSData dataWithContentsOfURL:url];
It looks like this:
2017-08-02 12:00:52.242397+0530 Trainer[608:107784] data=<0a0a2246 61696c65 6420746f 20526563 6f766572 20796f75 72207061 7373776f 72642c20 74727920 61676169 6e22>
Attempting to convert it to a string with one of the code below returns nil:
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString* newStr = [NSString stringWithUTF8String:[data bytes]];

NSString plain encoded with NSASCIIStringEncoding to NSData

I packed an object (NSObject) to (NSData) and then encoded it with (NSASCIIStringEncoding) for sending it to a SQLite database with this code:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:canvasView.trazoYorch];
//convert NSData object to plain text for sending it to DB
NSData *data2 = [[NSString stringWithFormat:#"%#",data] dataUsingEncoding:NSASCIIStringEncoding];
NSString *dataStr = [[NSString alloc] initWithData:data2 encoding:NSASCIIStringEncoding];
everything works ok, but when I want to do the Inverse process NSString to NSData I got different results, this is my code for inverse process
NSString *FirmaString = [self traerFirmadeBD]; //returns the string content of DB
NSData *data2 = [FirmaString dataUsingEncoding:NSASCIIStringEncoding];
FirmaYorch *firmaCompleta = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
Any help solving this I will appreciate

Parse JSON file into POST request Objective C

I parse an JSON File into an Dictionary and in a further step I want to build a post request for couchDB.
The parsing works fine, but if I post I get an error. I thinks it has something to do with my escape sequence in the post string.
HereĀ“s the code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"json"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
// getting the data from file
NSString *_id = (NSString *) [data objectForKey:#"_id"];
NSString *rev_ = self.rev;
NSString *_herausgeber = (NSString *) [data objectForKey:#"Herausgeber"];
NSString *_nummer = (NSString *) [data objectForKey:#"Nummer"];
NSNumber *_deckung = [data objectForKey:#"Deckung"];
NSString *_waerhung = (NSString *) [data valueForKey:#"Waehrung"];
NSDictionary *_inhaber = (NSDictionary *) [data objectForKey:#"Inhaber"];
NSString *_name = (NSString *) [_inhaber objectForKey:#"Name"];
NSString *_vorname = (NSString *) [_inhaber objectForKey:#"Vorname"];
NSNumber *_maennlich = (NSNumber *) [_inhaber objectForKey:#"maennlich"];
NSArray *_hobbys = (NSArray *) [_inhaber objectForKey:#"Hobbys"];
NSString *_hobby0 = [_hobbys objectAtIndex:0];
NSString *_hobby1 = [_hobbys objectAtIndex:1];
NSString *_hobby2 = [_hobbys objectAtIndex:2];
NSNumber *_alter = (NSNumber *) [_inhaber objectForKey:#"Alter"];
NSArray * _kinder = (NSArray *) [_inhaber objectForKey:#"Kinder"];
NSString *_kind0 = [_kinder objectAtIndex:0];
NSString *_kind1 = [_kinder objectAtIndex:1];
NSString *_kind2 = [_kinder objectAtIndex:2];
NSString *_partner = (NSString *) [_inhaber objectForKey:#"Partner"];
[parser release];
//post string:
NSString *post = [NSString stringWithFormat:#"{\"_id\":\"%#\",\"_rev\":\"%#\",\"Herausgeber\":\"%#\",\"Nummer\":\"%#\",\"Deckung\":%#,\"Waehrung\":\"%#\",\"Inhaber\":{\"Name\":\"%#\",\"Vorname\":\"%#\",\"maennlich\":%#,\"Hobbys\":[\"%#\",\"%#\",\"%#\"],\"Alter\":%#,\"Kinder\":[\"%#\",\"%#\",\"%#\"],\"Partner\":%#}}",_id,rev_,_herausgeber,_nummer,_deckung,_waerhung,_name,_vorname,_maennlich,_hobby0,_hobby1,_hobby2,_alter,_kind0,_kind1,_kind2,_partner];
//post header:
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:cdbURL]];
[request setHTTPMethod:#"PUT"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
The Error I get:
Reply: {"error":"bad_request","reason":"invalid UTF-8 JSON:
<<\"{\\"_id\\":\\"161eba7093799b610502bdfba5004281\\",\\"_rev\\":\\"199-bb065cbd0a365b188fc492cc22453d74\\",\\"Herausgeber\\":\\"SAP\\",\\"Nummer\\":\\"1234-5678-9012-3456\\",\\"Deckung\\":2000000,\\"Waehrung\\":\\"Franc\\",\\"Inhaber\\":{\\"Name\\":\\"Mustermann\\",\\"Vorname\\":\\"Max\\",\\"maennlich\\":1,\\"Hobbys\\":[\\"Reiten\\",\\"Golfen\\",\\"Lesen\\"],\\"Alter\\":42,\\"Kinder\\":[\\"Max\\",\\"Moritz\\",\\"Lisa\\"],\\"Partner\\":}}\">>"}
How can I solve this problem?
Personally, I would avoid constructing the JSON post-string myself and instead use SBJson's [NSObject JSONRepresentation] function.
Then you could fill an NSDictionary and have the API construct the JSON for you.
NSString *_id = #"161";
NSString *_rev = #"199";
NSString *_herausgeber = #"SAP";
NSMutableDictionary *toPost = [NSMutableDictionary dictionary];
[toPost setObject:_id forKey:#"_id"];
[toPost setObject:_rev forKey:#"_rev"];
[toPost setObject:_herausgeber forKey:#"Herausgeber"];
NSLog(#"JSON: %#", [toPost JSONRepresentation]);
Gives:
JSON: {"_id":"161","_rev":"199","Herausgeber":"SAP"}

Convert NSData to a NSURL

I have seen this done the other way. But I have a NSData object returned that is suppose to be a URL of an image file.
How do I do this convert a NSData object into a NSURL?
Thanks
You first need to convert the NSData object to an NSString object and then you can just create a NSURL with the new string.
NSString *urlString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // Or any other appropriate encoding
NSURL *url = [[NSURL alloc] initWithString:[urlString autorelease]];