NSData to NSString conversion return nil - objective-c

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

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

Is it possible to read and write to a .h file with objective c?

I'm trying to make an xcode plugin that needs to write something to the .h file from the .m file. I haven't been able to find a stackoverflow post with an answer on how to do this. I've tried this code
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSString *contents = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
I've also tried using the StringWithContentsOfFile method, however they both return nil. Is it because you can't read an .h file with this code or is it something else I'm missing.
filePath is this and it's a correct filepath, my ftp client can read it atleast file:///Users/myUserName/Documents/code/macOsDev/XcodePlugIns/XcoderPlugin/XcoderPlugin/XcoderPlugin.h
So my question is, how do I read and write to a .h file? Thanks in advance.
EDIT as requested, some more code however this is as far as I've gotten to the read/write part of my plugin.
NSString *filePath = path;
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSString *contents = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
NSError *error;
NSString *contents1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
contents is #"" because data is nil
and contents1 is just nil;
error is error
NSError * domain: #"NSCocoaErrorDomain" - code: 260
in the debugger, but I'm not sure I'm using this error thing correctly.
Use "/Users/myUserName/Documents/code/macOsDev/XcodePlugIns/XcoderPlugin/XcoderPlugin/XcoderPlugin.h".
Dont use 'file://' prefix
Following is the code for reading and writing
NSString *path = #"your/path/tp/.h/file";
NSData *data = [NSData dataWithContentsOfFile:path];
//Get the contents of the file into the mutable string
NSMutableString *contents = [[NSMutableString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
//Make changes to your mutable string
[contents appendString:#"abc"];
//Write it back to the file
[contents writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

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

NSJSONSerialization: treating special characters properly

I want to use online data from a JSON file. It's currently working, but when I use special characters it shows "null". Here's the code I'm using:
NSString *urlString = [NSString stringWithFormat:#"http://belesios.com/burc/koc2.php"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"%#", json);
NSString *ciktiText = [[NSString alloc] initWithFormat:#"%#", json];
[kocLabel setText:ciktiText];
For example, if my file contains ç or ü, null is returned. What do I need to do?
Try this method to find out what encoding is used when data is pulled from server:
NSStringEncoding encoding;
NSString *jsonString =[NSString stringWithContentsOfURL:URL usedEncoding:&encoding error:&error];
if (error)
NSLog(#"Error: %#", error);
You should check encoding of data returned by the server. Your code is correct but you should add some check if error != nil then don't execute and display or log that.
NSString *jsonString = #"{\"abc\":\"ç or ü\"}";
NSDictionary *jsonObj = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
NSLog(#"Output: %#", jsonObj);
Output: { abc = "\U00e7 or \U00fc"; } This is how your server response should look like with encoded char if you use Fiddler or some other software to print raw response.
Currently your data looks like where as UTF8 encoded chars should be like \U00e7 = ç

Conversion of NSData to NSString?

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