HTML character decoding in Objective-C / Cocoa Touch

cocoa, cocoa-touch, html, iphone, objective-c

Solution

Those are called Character Entity References. When they take the form of `&#<number>;` they are called numeric entity references. Basically, it's a string representation of the byte that should be substituted. In the case of `&#038;`, it represents the character with the value of 38 in the ISO-8859-1 character encoding scheme, which is `&`.

The reason the ampersand has to be encoded in RSS is it's a reserved special character.

What you need to do is parse the string and replace the entities with a byte matching the value between `&#` and `;`. I don't know of any great ways to do this in objective C, but this stack overflow question might be of some help.

Edit: Since answering this some two years ago there are some great solutions; see @Michael Waterfall's answer below.

Problem

First of all, I found this: Objective C HTML escape/unescape, but it doesn't work for me. My encoded characters (come from a RSS feed, btw) look like this: `&#038;` I searched all over the net and found related discussions, but no fix for my particular encoding, I think they are called hexadecimal characters.

Original source

Related problems