decoding problem uiwebview - iphone

i am using uiwebview in my application. there are some links when user clicks a http search starts. it works fine but i have problems while getting "%58 den ysnky'ye tepki" it is given as "X'den ysnky'ye tepki". it has problems with % char.
identifier:%58'den%20ysnky'ye%20tepki
decoded identifier:X'den ysnky'ye tepki
i am using stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding to decode the string like that;
NSLog(#"identifier:%#", identifier);
identifier = [identifier stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"decoded identifier:%#", identifier);
how can i get the correct string?
thanks...

It looks like your string may not be encoded properly in the first place. %58 is the correct encoding for the letter “X” (see this ASCII table). As far as I can tell, therefore, the decode is behaving properly.
What are you expecting?

Related

My qrcode scanner not able to scan specific URL containing special character

I am able to scan all qrcodes for the urls however some urls containing special characters such as "%20" are not getting scanned and it crashes the app. I am using ZBarSDK for scanning and ZBarReaderView is the scanner.
http://www.winlogisticsmedia.com/images/bigkmr%20end%20sale.jpg is the URL which when made on qrcode, my app crashes and it shows the below window.
P.S: One more thing is that I think on which I am getting the scanning report (text) is a nsstring. Is there any chance relating to it as its an string and the text consists of numerics and special characters?
I was having the same issue. My app would crash when I would log the AVMetadataMachineReadableCodeObject stringValue.
My original code that would crash:
NSLog("%#", [machineReadableCodeObject stringValue]);
Once I decoded the stringValue with stringByReplacingPercentEscapesUsingEncoding it does not crash anymore:
NSString* decodedValue = [[machineReadableCodeObject stringValue] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("%#", decodedValue);

Data encoding in Objective-C and Ruby

I'm working on an iOS app using Parse, and I need to simulate their encryption flow using Ruby for the website and browser extensions I'm building for it.
For generating the salt for AES, they have the following code in the iOS app:
NSString *str = user.email;
NSData *strData = [str dataUsingEncoding: NSUTF8StringEncoding];
NSData *encryptedData = [strData AESEncryptWithPassphrase:str]; // using the same key
What's puzzling me is dataUsingEncoding: NSUTF8StringEncoding -- it's obviously encoding the string in UTF-8, but when I see it in NSLog it seems quite different. So if str was "randomemail#gmail.com", doing NSLog on strData outputs:
<72616e64 6f6d656d 61696c40 676d6169 6c2e636f 6d>
Now, how do I get that same output in Ruby? I can't get the right salt because of this ("randomemail#gmail.com".encode("UTF-8") simply returns the email as expected). How can I simulate dataUsingEncoding in Ruby to get the same exact salt, is there something I'm missing?
Thanks
Try this:
"randomemail#gmail.com".encode("UTF-8").bytes.to_a.map{ |x| x.to_s(16)}
and you will "see" what you want.
The hexadecimal representation of "randomemail#gmail.com" with UTF-8 encoding is
<72616e64 6f6d656d 61696c40 676d6169 6c2e636f 6d>
But ruby shows you the string representation, which is "randomemail#gmail.com".
They are showing the same stuff in different ways.
Check here for more information.

CFURLCreateStringByAddingPercentEscapes, strange behavior?

I am trying to encode a URL, I've never done this before, so I'm confused when not getting the results expected.
I'm using CFURLCreateStringByAddingPercentEscapes to do this, but whats returning looks nothing like any online URL encoders/decoders e.g.
-(void)urlEncodedString{
NSString *str = #"\"Hi!! my name is John. \n What's your's?\"";
NSLog([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)str, NULL, CFSTR("!$&'()*+,-./:;=?#_~"), kCFStringEncodingUTF8) autorelease]);
}
I was expecting something like:
%5C%22Hi%21%21%20my%20name%20is%20John.%20%5Cn%20What%27s%20your%27s%3F%5C%22
But instead I'm getting:
2i2212yame 0s2ohn3.786691E-27020A2hat º»åå2our 0.0000002
That can't be normal. I've been searching and tried everything, the way I did it apparently should work.
Can anyone point me in the right direction?
You are passing the result as the first string to NSLog which expects a string with formatting which uses the percent signs. You are essentially filling the string with random data in memory in place of each % escape. To fix this log using an Objective-C object specifier:
NSLog(#"%#", [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)str, NULL, CFSTR("!$&'()*+,-./:;=?#_~"), kCFStringEncodingUTF8) autorelease]);

'?' in strings breaking my XML parser

The parsing of a file containing XML breaks when ever there is a string containing '?'. As an example you can see the line below.
<Radio id="32">
<stationName>BBC 5 Live</stationName>
<streamType>aac+</streamType>
<streamBandwidth>48kbps</streamBandwidth>
<streamURL>http://bbcmedia.ic.llnwd.net/stream/bbcmedia_he2_5live_q?s=1308038932&e=1308053332&h=868e4fa343b375695183f6a3bd0267d9</streamURL>
</Radio>
Is there some way to encode the '?' or what is the way thats generally used to handle this kind of problem, as I would imagine this would be encountered a lot.
The line of code that handles this (i believe) is :
[aRadio setValue:currentElementValue forKey:elementName];
Though I maybe that is not where it breaks.
Many Thanks,
-Code
Most probably, the problem is not the '?' character, but the '&' characters in your URL. The '&' character has a special meaning in XML (it is used to start entities like & or <), so the XML parser will fail if it doesn't find a valid entity. The way to go is to wrap the value in a CDATA section as deanWombourne suggests.
Try this. Convert it into UTF8
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
Then parse with this data...
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
There's a few ways of dealing with this.
1) Percent encode the string before sending from the server (your '?' would become '%3F') and decode the data when you receive in in your app. - see this answer for more details.
2) Use CDATA markers around this bit of data - see here for more details

NSData to NSString using initWithBytes:length:encoding

I have some image data (jpeg) I want to send from my iPhone app to my webservice. In order to do this, I'm using the NSData from the image and converting it into a string which will be placed in my JSON.
Currently, I'm doing this:
NSString *secondString = [[NSString alloc] initWithBytes:[result bytes]
length:[result length]
encoding:NSUTF8StringEncoding];
Where result is of type NSData. However, secondString appears to be null even though result length returns a real value (like 14189). I used this method since result is raw data and not null-terminated.
Am I doing something wrong? I've used this code in other areas and it seems to work fine (but those areas I'm currently using it involve text not image data).
TIA.
For binary data, better to encode it using Base64 encoding then decode it in you webservice. I use NSData+Base64 class downloaded from here, this reference was also taken from Stackoverflow, an answer made by #Ken (Thanks Ken!).
You are not converting the data to a string. You are attempting to interpret it as a UTF-8 encoded string, which will fail unless the data really is a UTF-8 encoded string. Your best bet is to encode it somehow, perhaps with Base64 as Manny suggests, and then decode it again on the server.