{"id":18795,"date":"2017-09-01T20:49:31","date_gmt":"2017-09-02T00:49:31","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18795"},"modified":"2017-09-01T20:49:31","modified_gmt":"2017-09-02T00:49:31","slug":"decoding-nsasciistringencoding-is-not-strict","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/09\/01\/decoding-nsasciistringencoding-is-not-strict\/","title":{"rendered":"Decoding NSASCIIStringEncoding Is Not Strict"},"content":{"rendered":"<p><a href=\"http:\/\/lapcatsoftware.com\/articles\/nsstringencoding.html\">Jeff Johnson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/lapcatsoftware.com\/articles\/nsstringencoding.html\">\n<p>The documentation for <code>NSASCIIStringEncoding<\/code> is clearly false: &ldquo;Strict 7-bit ASCII encoding within 8-bit chars; ASCII values 0&#8230;127 only.&rdquo; The <code>NSString.h<\/code> header file contains the same falsehood:<\/p>\n<pre>NSASCIIStringEncoding = 1, \/* 0..127 only *\/<\/pre>\n<p>Curiously, though, the <code>CFString.h<\/code> header file has a more useful comment:<\/p>\n<pre>kCFStringEncodingASCII = 0x0600, \/* 0..127 (in creating CFString, values\ngreater than 0x7F are treated as corresponding Unicode value) *\/<\/pre>\n<\/blockquote>\n<p>Another oddity with the documentation for <code>NSString<\/code> decoding: it only says that <code>-initWithBytes:length:encoding:<\/code> returns <code>nil<\/code> if the byte string is too long, but it says that <code>-initWithData:encoding:<\/code> returns <code>nil<\/code> if the data is not valid for the encoding. You would think these methods would be consistent.<\/p>\n<p>However, I have found that <code>NSASCIIStringEncoding<\/code> does work as expected when <em>encoding<\/em>. That is, if the string is non-ASCII it will give you a <code>nil<\/code> data unless you request a lossy conversion.<\/p>","protected":false},"excerpt":{"rendered":"<p>Jeff Johnson: The documentation for NSASCIIStringEncoding is clearly false: &ldquo;Strict 7-bit ASCII encoding within 8-bit chars; ASCII values 0&#8230;127 only.&rdquo; The NSString.h header file contains the same falsehood: NSASCIIStringEncoding = 1, \/* 0..127 only *\/ Curiously, though, the CFString.h header file has a more useful comment: kCFStringEncodingASCII = 0x0600, \/* 0..127 (in creating CFString, values [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,861,164,31,1380,30,1381,54,71,258],"class_list":["post-18795","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-core-foundation","tag-documentation","tag-ios","tag-ios-10","tag-mac","tag-macos-10-12","tag-objective-c","tag-programming","tag-unicode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=18795"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18795\/revisions"}],"predecessor-version":[{"id":18796,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18795\/revisions\/18796"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}