decode base64 string as UTF-8 - iphone

I am using the base64 implementation at the bottom of this post.
If I use following code:
NSLog(#"decoded:%#",[[[NSString alloc] initWithData:[Base64 decode:#"8fEmIzEyNDA3OyYjMTI0MTE7"] encoding:NSUTF8StringEncoding] autorelease]);
I get decoded:(null)
However, if I use:
NSLog(#"decoded 1:%#",[[[NSString alloc] initWithData:[Base64 decode:#"8fEmIzEyNDA3OyYjMTI0MTE7"] encoding:NSASCIIStringEncoding] autorelease]);
I get decoded:ññぷほ
But I should get decoded:ññぷほ
What am I doing wrong?

Those are HTML character references. You'll need to decode further if you want raw text.

You should read this article by Matt Gallagher. At the bottom there is link with code for iOS if that is what you are after.
It provides an class extension to NSData which is you string is easily converted from and too.

Related

Converting a photo data to nsstring returns nil

I'm allowing the users of my app to either take a pic or select one from their library. When selected I need to get the images' data and convert it to a string so I can send it to a web service.
The problem I'm currently having is that [NSString initWithData:] is returning nil when I have the encoding set to UTF8. I need to set it to UTF8 for the XML message.
NSString *dataString = [[NSString alloc] initWithData:UIImageJPEGRepresentation(theImage, 1.0) encoding:NSUTF8StringEncoding];
Thanks for any advice!
You can't convert an UIImage to NSString by using initWithData:encoding: method. This method is only for converting an string's data to NSString (an Text File for example).
If you are trying to convert any kind of binary data to NSString, there are some kind of encoding available. Base64 is widely used. Also your server should have the ability to decode what you've sent.
In addition, in most cases, send an image to server just need to POST it in binary as it used to be.

Image to Text (iPhone)

How can I get the texts presented on an image ?
Description : "I am developing one application which needs to get the text presented on an Image and show the text in an another view after putting the texts in to some frames."
EDIT : From this I'm getting some Values and also the ".png" but the characters I am getting is like ASCII value or something.
UIImage*img=[UIImage imageNamed:#"btn_reset.png"];
NSData *dataObj = UIImagePNGRepresentation(img);
NSString*str= [[NSString alloc] initWithData:dataObj encoding:NSASCIIStringEncoding];
printf("Image is %s",[str UTF8String]);
Any tutorial/link/solution would be very helpful.
Thank You !
I couldn't understand the question clearly, but I think you are referring to OCR.
One of the main open-source lib for this is: Tesseract
Some one has ported this lib for ios. You can get the code from: https://github.com/rcarlsen/Pocket-OCR/
you might get solution from following link:
Is there any iphone Class that converts images to text format?
If you just want to print the string
UIImage*img=[UIImage imageNamed:#"btn_reset.png"];
NSData *dataObj = UIImagePNGRepresentation(img);
NSString*str= [[NSString alloc] initWithData:dataObj encoding:NSASCIIStringEncoding];
NSLog("Image is %#",str);
If you still dont get it change the encoding

'?' 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.

Convert NSData [UIImage] to a NSString

I am aware this question has been asked several times, but I was unable to find a definate answer that would best fit my situation.
I want the ability to have the user select an image from the library, and then that image is converted to an NSData type. I then have a requirement to call a .NET C# webservice via a HTTP get so ideally I need the resulting string to be UTF8 encoded.
This is what I have so far:
NSData *dataObj = UIImageJPEGRepresentation(selectedImage, 1.0);
[picker dismissModalViewControllerAnimated:YES];
NSString *content = [[NSString alloc] initWithData:dataObj encoding:NSUTF8StringEncoding];
NSLog(#"%#", content);
The NSLog statement simply produces output as:
2009-11-29 14:13:33.937 TestUpload2[5735:207] (null)
Obviously this isnt what I hoped to achieve, so any help would be great.
Kind Regards
You can't create a UTF-8 encoded string out of just any arbitrary binary data - the data needs to actually be UTF-8 encoded, and the data for your JPEG image obviously is not. Your binary data doesn't represent a string, so you can't directly create a string from it - -[NSString initWithData:encoding:] fails appropriately in your case.
Assuming you're using NSURLConnection (although a similar statement should be true for other methods), you'll construct your NSMutableURLRequest and use -setHTTPBody: which you need to pass an NSData object to. I don't understand why you would be using a GET method here since it sounds like you're going to be uploading this image data to your web service - you should be using POST.