Reputation: 15
I have some NSData output that I would like to convert to a string.
NSString * test = [NSString stringWithFormat:@"myfile.txt];
NSData *myData = [[NSData alloc] initWithContentsOfFile:(@"%@", test)];
This NSData was saved to a file, it looks like : 21fa9731 27c67c00 da1c3349 d82470eb 56f97b88 559f406c 6abecbb7 de020007 47a4541d 99c9c5e7 883f8bf1 165fba39
Do you know a way to get this string back as it was in "myfile.txt" ?
Thanks !
Upvotes: 0
Views: 1135
Reputation: 1461
If data file somewhere on HDD (not in app bundle) you must provide full path to your data file.
NSString * test = [NSString stringWithFormat:@"myfile.txt];
NSData *myData = [[NSData alloc] initWithContentsOfFile:(@"%@", test)];
NSString* myString = [NSString alloc]initWithData:myData
encoding: NSUTF8StringEncoding];
Choose encoding in which you save you data file.
Upvotes: 1
Reputation: 14068
I don't know what your problem ist. You could share with us how the file was written so that we can get a bit closer.
However, you are doing much too complicated and therefore error prone. The following would to for reading a file with a constant name fo myfile.txt
.
NSData *myData = [[NSData alloc] initWithContentsOfFile:@"myfile.txt"];
Frankly I don't even know in which directory myfile.txt
is expected to be. And there are of course much better ways to deal with constant literals than using literals directly in the code.
BTW, what do you actually receive in myData? null?
(This is more of a comment than an answer, but the formatting is much better in answers.)
Upvotes: 0