3

How to determine string encoding in cocoa? Recently I'm working on a radio player.Sometimes id3 tag text was garbled.

Here is my code:

CFDictionaryRef audioInfoDictionary;
UInt32 size = sizeof(audioInfoDictionary);
result = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &size, &audioInfoDictionary);

ID3 info are in audioInfoDictionary. Sometimes the id3 doesn't use utf8 encoding, and title, artist name were garbled.

Is there any way to determine what encoding a string use?

Special thx!

OpenThread
  • 2,096
  • 3
  • 28
  • 49
  • What do you mean -- the encoding of NSStrings (Unicode), or the character translation used from 8-bit chars to NSStrings (many different ones), or the encoding of the id3 (it depends on the encoding byte), or the character set supported by the display? – Hot Licks May 20 '12 at 14:17
  • This previous question may help you: http://stackoverflow.com/questions/6948376/is-there-a-way-to-auto-detect-the-encoding-of-a-resource-when-loading-it-using – mttrb May 20 '12 at 14:41
  • i think you can't get the encoding description in normal way. may some one has the trick how to get it. but you can check whether ur string suports some encoiding or not using this - (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding; – Saad May 20 '12 at 14:59
  • I want to detect the encoding of the id3, and canBeConvertedToEncoding: looks helpful – OpenThread May 21 '12 at 03:44

1 Answers1

3

While it's an NSString object, there's no specific encoding since it's guaranteed to represent whatever is put into it using the encoding determined when it was created. See the Working With Encodings section of the docs.

From where are you getting the ID3 tags? The time you "receive" this information is the best time to determine its encoding. See Creating and Initializing Strings and the next few sections (for file and url creation) for a list of initializers. Some of them let you set the encoding and others pass back (by reference) the "best guess" encoding the system determined when creating the string. Look for methods with "usedEncoding:" for the system's reported guess.

All of this really depends on exactly what is handing you that string. Are you reading it from a file (an MP3) or a web service (Internet Radio)? If the latter, the server's response should include the encoding and if that's wrong, there's not much to do but guess.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • I showed the code i used to get id3 infomation in question. ID3 was read from an MP3 file. The infomation dictionary contains garbled text, and i don't know what encoding do they use.And which methods pass back the "best guess"? What i need are these guesses. Thanks! – OpenThread May 21 '12 at 04:16