{"id":1558,"date":"2012-09-17T20:58:42","date_gmt":"2012-09-17T16:58:42","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=1558"},"modified":"2012-09-18T15:12:01","modified_gmt":"2012-09-18T11:12:01","slug":"how-to-change-string-encoding-from-koi8-r-to-utf8-in-objective-c","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=1558","title":{"rendered":"How to Change String Encoding from KOI8-R to UTF8 in Objective-C"},"content":{"rendered":"<p>KOI8-R is an 8-bit character encoding, designed to cover Russian, which uses the Cyrillic alphabet. I was getting a fileName from IMAP in this encoding and wanted to convert it to normal readable UTF-8.<\/p>\n<p>My question concerning this issue <a href=\"http:\/\/stackoverflow.com\/questions\/12461711\/decoding-a-string-in-objective-c\" title=\"http:\/\/stackoverflow.com\/questions\/12461711\/decoding-a-string-in-objective-c\" target=\"_blank\">in stackoverflow was banned<\/a>. I had to solve this myself and spent quite a bit of time. <\/p>\n<p>There is one solution, that works for many encodings &#8211; to use <a href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/CoreFOundation\/Reference\/CFStringRef\/Reference\/reference.html\" title=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/CoreFOundation\/Reference\/CFStringRef\/Reference\/reference.html\" target=\"_blank\">CFString methods<\/a>:<br \/>\n<pre class=\"alwawee-code\"><code>&lt;br \/&gt;\n\u2013 (NSString*)decodeKOI8R:(NSString*)stringToDecode {&lt;\/p&gt;\n&lt;p&gt;    CFStringRef aCFString = CFStringCreateWithCString(NULL, [stringToDecode UTF8String], kCFStringEncodingKOI8_R);&lt;br \/&gt;\n    NSString *decodedString = (__bridge NSString*)aCFString;&lt;\/p&gt;\n&lt;p&gt;    return decodedString;&lt;br \/&gt;\n}&lt;br \/&gt;\n<\/code><\/pre><\/p>\n<p>But for some reason it worked for UTF-7 but didn&#8217;t for KOI8-R. <\/p>\n<p>I searched Google for hours, but couldn&#8217;t find a solution. Suddenly, I found a solution in MailCore CTBareAttachment class method, called decodedFilename. <\/p>\n<p>This is ObjC code of that method:<br \/>\n<pre class=\"alwawee-code\"><code>&lt;br \/&gt;\n-(NSString*)decodedName {&lt;br \/&gt;\n    return MailCoreDecodeMIMEPhrase((char *)[self.name UTF8String]);&lt;br \/&gt;\n}&lt;br \/&gt;\n<\/code><\/pre><\/p>\n<p>This is a C language method, that converts char* to the NSString with a proper encoding.<br \/>\n<pre class=\"alwawee-code\"><code>&lt;br \/&gt;\nNSString *MailCoreDecodeMIMEPhrase(char *data) {&lt;br \/&gt;\n    int err;&lt;br \/&gt;\n    size_t currToken = 0;&lt;br \/&gt;\n    char *decodedSubject;&lt;br \/&gt;\n    NSString *result;&lt;\/p&gt;\n&lt;p&gt;    if (*data != \u2018\\0\u2019) {&lt;br \/&gt;\n        err = mailmime_encoded_phrase_parse(DEST_CHARSET, data, strlen(data),&lt;br \/&gt;\n                                            &amp;currToken, DEST_CHARSET, &amp;decodedSubject);&lt;\/p&gt;\n&lt;p&gt;        if (err != MAILIMF_NO_ERROR) {&lt;br \/&gt;\n            if (decodedSubject == NULL)&lt;br \/&gt;\n                free(decodedSubject);&lt;br \/&gt;\n            return nil;&lt;br \/&gt;\n        }&lt;br \/&gt;\n    } else {&lt;br \/&gt;\n        return @\u201d\u201d;&lt;br \/&gt;\n    }&lt;\/p&gt;\n&lt;p&gt;    result = [NSString stringWithCString:decodedSubject encoding:NSUTF8StringEncoding];&lt;br \/&gt;\n    free(decodedSubject);&lt;br \/&gt;\n    return result;&lt;br \/&gt;\n}&lt;br \/&gt;\n<\/code><\/pre><\/p>\n<p>Here<br \/>\n<pre class=\"alwawee-code\"><code>&lt;br \/&gt;\n#define DEST_CHARSET \u201cUTF-8\u201d&lt;br \/&gt;\n<\/code><\/pre><\/p>\n<p>and mailmime_encoded_phrase_parse is a pretty complex method, that is not easy even to copy paste here. So, this problem seems to have no easy solution, except using this C method. Actually this method parses a phrase and calls decoding C method for each word. And I ended up using it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>KOI8-R is an 8-bit character encoding, designed to cover Russian, which uses the Cyrillic alphabet. I was getting a fileName from IMAP in this encoding and wanted to convert it to normal readable UTF-8. My question concerning this issue in stackoverflow was banned. I had to solve this myself and spent quite a bit of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1558","post","type-post","status-publish","format-standard","hentry","category-ios"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Change String Encoding from KOI8-R to UTF8 in Objective-C - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=1558\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Change String Encoding from KOI8-R to UTF8 in Objective-C - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"KOI8-R is an 8-bit character encoding, designed to cover Russian, which uses the Cyrillic alphabet. I was getting a fileName from IMAP in this encoding and wanted to convert it to normal readable UTF-8. My question concerning this issue in stackoverflow was banned. I had to solve this myself and spent quite a bit of [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=1558\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-17T16:58:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-09-18T11:12:01+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"How to Change String Encoding from KOI8-R to UTF8 in Objective-C\",\"datePublished\":\"2012-09-17T16:58:42+00:00\",\"dateModified\":\"2012-09-18T11:12:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558\"},\"wordCount\":303,\"commentCount\":0,\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558\",\"name\":\"How to Change String Encoding from KOI8-R to UTF8 in Objective-C - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2012-09-17T16:58:42+00:00\",\"dateModified\":\"2012-09-18T11:12:01+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1558#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Change String Encoding from KOI8-R to UTF8 in Objective-C\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Change String Encoding from KOI8-R to UTF8 in Objective-C - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=1558","og_locale":"en_US","og_type":"article","og_title":"How to Change String Encoding from KOI8-R to UTF8 in Objective-C - Denis Kutlubaev","og_description":"KOI8-R is an 8-bit character encoding, designed to cover Russian, which uses the Cyrillic alphabet. I was getting a fileName from IMAP in this encoding and wanted to convert it to normal readable UTF-8. My question concerning this issue in stackoverflow was banned. I had to solve this myself and spent quite a bit of [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=1558","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2012-09-17T16:58:42+00:00","article_modified_time":"2012-09-18T11:12:01+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=1558#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=1558"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"How to Change String Encoding from KOI8-R to UTF8 in Objective-C","datePublished":"2012-09-17T16:58:42+00:00","dateModified":"2012-09-18T11:12:01+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=1558"},"wordCount":303,"commentCount":0,"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=1558#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=1558","url":"https:\/\/blog.alwawee.ru\/?p=1558","name":"How to Change String Encoding from KOI8-R to UTF8 in Objective-C - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2012-09-17T16:58:42+00:00","dateModified":"2012-09-18T11:12:01+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=1558#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=1558"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=1558#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"How to Change String Encoding from KOI8-R to UTF8 in Objective-C"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1558"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1558\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}