{"id":676,"date":"2012-11-27T18:59:18","date_gmt":"2012-11-27T18:59:18","guid":{"rendered":"http:\/\/www.bardecode.com\/en1\/?p=676"},"modified":"2013-03-05T11:17:54","modified_gmt":"2013-03-05T11:17:54","slug":"double-encoded-utf-8-strings-in-c","status":"publish","type":"post","link":"https:\/\/www.bardecode.com\/en1\/double-encoded-utf-8-strings-in-c\/","title":{"rendered":"Double encoded UTF-8 strings in C#"},"content":{"rendered":"<p>This article shows how to convert a string that has been double encoded using UTF-8.<\/p>\n<p>For example, say you have the string M\u00c3\u00bcller instead of the string M\u00fcller.<\/p>\n<p><strong>How did it happen?<\/strong><\/p>\n<p>The letter\u00a0\u00fc is encoded in UTF-8 as 2 bytes: 195 and 188<\/p>\n<p>If you encoded the bytes again then the 195 converts to 195 and 131 which is the\u00a0\u00c3<\/p>\n<p>And the 188 converts to 194 and 188 which is the\u00a0\u00bc<\/p>\n<p><strong>How can it be converted back to what it should look like?<\/strong><\/p>\n<p>The following function will convert the double encoded string back to the original value&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>private\u00a0string\u00a0decodeUTF8String(String\u00a0utf8Str)<\/p>\n<p>{<\/p>\n<p>System.Text.Encoding\u00a0iso = System.Text.Encoding.GetEncoding(&#8220;ISO-8859-1&#8221;);<\/p>\n<p>System.Text.Encoding\u00a0utf8 = System.Text.Encoding.UTF8;<\/p>\n<p>byte[] utfBytes = utf8.GetBytes(utf8Str);<\/p>\n<p>byte[] isoBytes = System.Text.Encoding.Convert(utf8, iso, utfBytes);<\/p>\n<p>System.Text.UTF8Encoding\u00a0encoding =\u00a0new\u00a0System.Text.UTF8Encoding();<\/p>\n<p>return\u00a0encoding.GetString(isoBytes);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How does this relate to barcodes?<\/strong><\/p>\n<p>Some PDF-417 barcodes may contain data that has already been encoded using UTF-8 and when we read the barcode we encode it again using UTF-8, giving a double encoded string. In the win32 DLL interface the work-around is simply to set the Encoding property to 0, but the above is necessary in the .Net interface.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to convert a string that has been double encoded using UTF-8. For example, say you have the string M\u00c3\u00bcller instead of the string M\u00fcller. How did it happen? The letter\u00a0\u00fc is encoded in UTF-8 as 2 bytes: 195 and 188 If you encoded the bytes again then the 195 converts to<\/p>\n<p><a href=\"https:\/\/www.bardecode.com\/en1\/double-encoded-utf-8-strings-in-c\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":326,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[196,76,31],"tags":[19,90,201,202,176],"class_list":["post-676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bardecodefiler","category-settings","category-software-development-kits","tag-barcode","tag-characters","tag-double-encoded","tag-special","tag-utf-8"],"_links":{"self":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/676"}],"collection":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":5,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"predecessor-version":[{"id":1662,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/676\/revisions\/1662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/media\/326"}],"wp:attachment":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}