SpiderElectron

Electronic Engineering Blog

Working with UTF8 Encoded strings in iOS

I designed a digital audio dock for a major client. The dock accepts both 30-Pin apple devices and Lightning-connector apple devices, in other words almost all iPods, iPhones and iPads. The dock has a network interface so that it can connect to, and be controlled by, other devices, such as another iOS device. The device can get track metadata, playlists and track lists from the dock.

During testing we noticed that on occasion the track name, artist name or album name would not display correctly. For example, the soundtrack album from the film “Les Misérables” would show as “Les Mis√©tables”.

This is because the device providing the meta data is using the UTF-8 encoding mechanism to encode special characters, i.e. those outside of the usual 8-bit ascii character set.

In our code we had were receiving the 8-bit string and encoding it to an NSString like this:

_strDockAlbum = [NSString stringWithFormat:@"%s",pContents];

where pContents was a pointer to the 8-bit data buffer.

To resolve the issue was simple, thanks to the stringWithUTF8String method of the NSString class.
The code now looks like this:

_strDockAlbum = [NSString stringWithUTF8String:pContents];

Simples!

, , ,

Leave a Reply