NSString plain encoded with NSASCIIStringEncoding to NSData - objective-c

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

Related

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

merge many bytes arrays into one byte array in objective c

i have Used NSMutableData to merge byte arrays
NSMutableData *payload;
payload = [[NSMutableData alloc] init];
[payload appendBytes:CFBridgingRetain((cm.msgBytes)) length:[cm.msgBytes length]];
NSString *cmdata = [[NSString alloc] initWithData:[payload mutableBytes] encoding:NSUTF8StringEncoding];
i want to keep on adding to payload until done
cmdata is always nil
as initWithData takes NSData so i converted the bytes to NSData but the result is still same
NSData *bytesData = [NSData dataWithBytes:[payload mutableBytes] length:[payload length]];
NSString *cmdata = [[NSString alloc] initWithData:bytesData encoding:NSUTF8StringEncoding];
I think you're confusing things in several places. Where an API says "bytes", it's talking about a C array of bytes; where it says "data", it's talking about an NSData object.
Assuming cm.msgBytes is an NSData object, which it appears to be given that you retrieve its .length, a better version of this code would be:
NSMutableData *payload;
payload = [[NSMutableData alloc] init];
[payload appendData:cm.msgBytes];
NSString *cmdata = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding];
If you wrote the class cm belongs to, you should probably rename msgBytes to msgData (or even messageData), to match the APIs better.
initWithData takes an NSData not bytes itself
NSString *cmdata = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding];

NSData to NSString conversion

I found this method to convert an NSData to an NSString object.
NSData *data = //some data
NSString *string = [NSString stringWithFormat:#"%#", data];
How will the data be decoded? Is NSUTF8StringEncoding applied?
Thank you!
This is not the recommended approach. Instead use:
NSString *newString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
This will ensure you have a specified encoding for the data.
In this case, the stringWithFormat: method will send NSData object the description message, get the result, and use that for the content of the newly created string. Essentially, the result is identical to
NSString *string = [data description];
According to NSData's documentation, description returns
An NSString object that contains a hexadecimal representation of the receiver’s contents in NSData property list format.
You probably should be using NSString *string = [[NSString alloc] initWithData: data encoding:SomeFormOfEncoding];
You can view the method details here.
The forms of encoding can be seen here.

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

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