views:

85

answers:

2

Hi all!

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?

+1  A: 

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

Ignacio Vazquez-Abrams
Does it mean that coding and decoding ぷぷ would not work since I would first get ぷぷ ?
dkk
No, you'd get `ぷぷ`.
Ignacio Vazquez-Abrams
But that should not be necessary since I encode an array of bytes, if I decode it again and see it as UTF-8, it should not be HTML encoded, should it?
dkk
Whatever you encode should decode to the same, unless something else gets in the way and re-encodes it for you.
Ignacio Vazquez-Abrams
The problem should be the way I convert the NSData to NSString, which is in the line of code I provided in the question. Can you find any problem there?
dkk
No, the problem is in the process that created the data in the first place. `>>> u'8fEmIzEyNDA3OyYjMTI0MTE7'.decode('base64')``'\xf1\xf1ぷほ'`
Ignacio Vazquez-Abrams
Also, that's not UTF-8, that's Latin-1.
Ignacio Vazquez-Abrams
I do not think ぷ is in latin1 =) latin1 is west-european characters
dkk
But it's *not* `ぷ`, it's `ぷ`.
Ignacio Vazquez-Abrams
yes, but the HTML encoding should not be used
dkk
Thanks a lot, you were right, it was the encoding (which btw was done by an online encoder/decoder)
dkk
A: 

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.

Thomas Børlum
I get the same output with M.G.'s code
dkk