I am downloading data (text) from a server.
I have tried with both:NSISOLatin1StringEncoding and NSASCIIStringEncoding
But I keep seeing things like: {"estado":"M\u00e9xico"}
Noting that it should read México and not M\u00e9xico (with an accent over the e).
Looking online I figured that \u00e9 is in fact é link.
But the NSString is not able to interpret this and instead prints weird things on my UILabels:
I would really really appreciate your help on this.
Alsso, if you are itnerested, you can download the data from here: http://www.miorden.com/demo/iphone/estadoJSON.php
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"]];
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"Downloaded: %#", string);
string = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"] encoding:NSISOLatin1StringEncoding error:nil];
NSLog(#"Downloaded: %#", string);
I have been literally trying for days and it is killing me!
Thank you so much!
That appears to be unicode, try NSUTF8StringEncoding.
The data is in JSON format, so you'll need to JSON decode it too.
For example using this: https://github.com/TouchCode/TouchJSON
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"]];
NSError *error;
NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:data error:&error];
NSLog(#"Test: %#", [[array objectAtIndex:11] valueForKey:#"estado"]);
outputs
2011-08-11 09:35:45.742 enctest[63236:407] Test: México
Related
I get a NSData object in return after decrypting a payload with aes128:
NSData *returnData = [ciphertext AES128DecryptWithKey:keyData withIV:ivData];
I get the following hex output when i try to NSLog this:
<2db88b73 d84599a1 5779c736 09c975b7 92750cf2 d11cb41b 19f13781
4401bc57 b2ad96c8 402e3ccf 851c0219 00aec76b>
I then try to setting it as NSString:
[[NSString alloc] initWithData:returnData
encoding:NSUTF8StringEncoding];
When using NSLog() on the string i get "(null)" as output.
Can someone tell me why and where i should look for the problem?
Collided with the same issue some time ago, found the answer here.
If the data is not null-terminated, you should use -initWithData:encoding:
NSString* newStr = [[[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding] autorelease];
If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
(If you have ARC enabled, remove the -autorelease call.)
I am doing this way. but i am not getting where is wrong.
NSString *strUrl = [NSString stringWithFormat:#"https://play.google.com/stor/apps/details?id=com.ShiftSharerfree_new&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5TaGlmdFNoYXJlcmZyZWVfbmV3Il"];
NSString *originalString = [NSString stringWithFormat:#"%#", strUrl];
NSData *data = [NSData dataFromBase64String:originalString];
NSString *data = [base64 originalString];
NSLog(#"data:%#", data);//[data base64EncodedString]);
Please guide me in above.
Perhaps you need a base64 conversion library, as mentioned here:
Convert between UIImage and Base64 string
I had a similar error with code for converting images to base64, using [base64 encode...] calls.
the first thing i'd noted is that the NSData and the NSString have the same name. Could this be the error?
I know this has been asked quite before, and I already followed couple of approaches, but they don't work.
Following is what I already tried:
NSString *newStr = [NSString stringWithUTF8String:[responseData bytes]];
NSString *newStr = [NSString stringWithFormat:#"%.*s", [responseData length], [responseData bytes]];
None of them works. In 1st case, it fills newStr with null. In 2nd, it fills with junk characters. I know from debugger log (po responseData) that I get valid response which is like bbbbbb 00 bbbbbb. [server sends them as byte array]
What to do?
EDIT:
I am receiving this data from http request response - using ASIHTTPRequest library, in case anybody can help on that line.
Try this,
NSData *responseData; [Initialize it]
NSString *receivedDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#",receivedDataString);
Please try following code
NSString *string = [[[NSString alloc] initWithData: responseData.bytes encoding:NSUTF8StringEncoding] autorelease];
You can use this code lines
NSString *str=[[NSString alloc] initWithBytes:data1.bytes length:data1.length encoding:NSUTF8StringEncoding];
I am posting this for records sake because I found a duplicate and voting to close this down.
Actually what I am receiving is a stream of bytes represented as hex, and all the answers indicated do not work. Only [NSData description] gave me true data, which is something I can't use because it is intended for debugging.
Finally I tried the solution given here, and I get what I want.
Thanks to all for trying to help out.
NSString *image1Data = [[NSData dataWithData:myData] encodeBase64ForData];
But for this, you have to use NSData+Base64Additions class.
Use following way
NSString *dC_Str = [[NSString alloc] initWithData:decryPtd_data encoding:NSASCIIStringEncoding] ;
When i try to parse xml containing email address say john#abc.com, it just shows "abc.com".
How can i make it to show the complete email address. In other cases i've removed some special charcters by using the following:-
string=[string stringByReplacingOccurrencesOfString:#"$" withString:#""];
but here i've to include the symbol "#" and characters before it.
Thanks for any help.
Well... finally found a solution for myself. I converted the xml data into a string and replaced characters. Below is the code:-
NSError* error;
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
content=[content stringByReplacingOccurrencesOfString:#"#" withString:#"#"];
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
Problem Solved :)
NSString *string = #"$amdocs.com";
string=[string stringByReplacingOccurrencesOfString:#"$" withString:#"#"];
NSLog(#"%#",string);
maybe it will help you.
I have upgraded from XCODE 4 to 4.2 and now i have problems.
The following code worked pre 4.2 to read the file in "filePath":
// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile;
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
// Load array
NSArray* myArray = [myString componentsSeparatedByString:#"\r"];
NSLog (#"\n \n Number of elements in myArray = %i", [myArray count]);
With 4.2 the "initWithContentsOfFile" in the following code line is deprecated:
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
...and should be replaced with the below according to the manual:
NSString *myString = [NSString stringWithContentsOfFile:filePath encoding: NSUTF8StringEncoding error:&err];
and i can not get this to read the records in the same file by replacing the code line. BTW, i have defined the &err.
When i NSLog myString i get (null).
I am getting a bit desperate to solve this and would very much appreciate any help.
Cheers
NSLog the err variable if there is an error. Also NSLog filePath.
Perhaps the encoding is not UTF-8, are you sure about the encoding?
The best non-UTF-8 encoding bet is NSMacOSRomanStringEncoding which supports 8-bit characters.
Try :
NSError* error = nil;
NSStringEncoding encoding;
NSString *fileContent = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:&error];
If that does not works, try in your code : NSASCIIStringEncoding
The file probably doesn't contain a UTF8 encoded string. See apple's documentation, which has an example of reading a file where you do not know the encoding: Reading data with an unknown encoding
You need to use the [string|init]WithContentsOfFile:usedEncoding:error method, and if that fails there are a few more things you can try before finally presenting an error message to the user (for example, try reading it as an NSAttributedString).
For example, you could do this:
// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile;
NSStringEncoding encoding;
NSError *error;
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath usedEncoding:&encoding error:&error];
if (!myString) {
myString = [[NSString alloc] encoding:NSUTF8StringEncoding error:&error]
}
if (!myString) {
myString = [[NSString alloc] encoding:NSISOLatin1StringEncoding error:&error]
}
if (!myString) {
NSLog(#"error: %#", error);
return;
}
// Load array
NSArray* myArray = [myString componentsSeparatedByString:#"\r"];
NSLog (#"\n \n Number of elements in myArray = %i", [myArray count]);