TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

What to know about encodings and character sets

83 pointsby dshankaralmost 10 years ago

12 comments

Animatsalmost 10 years ago
Something they should have mentioned: put a<p><pre><code> &lt;meta charset=&quot;utf-8&quot; &#x2F;&gt; </code></pre> in all your HTML documents that are in UTF-8. Note that this has to be in the first 1024 bytes of the document. Otherwise, the browser has to invoke the &quot;encoding guesser&quot;[1], which will sometimes guess wrong. (W3C: &quot;The user agent may attempt to autodetect the character encoding from applying frequency analysis or other algorithms to the data stream.&quot;) The result will be occasional users seeing random pages in the wrong encoding, depending on browser, browser version, platform, and page content.<p>I recently saw the front page of the New York Times misconverted because they didn&#x27;t specify an encoding, and the only UTF-8 sequence near the beginning of the document was in<p><pre><code> &lt;link rel=&quot;apple-touch-icon-precomposed&quot; sizes=&quot;144×144&quot; ... </code></pre> The &quot;×&quot; there is not the letter x, it&#x27;s the Unicode multiplication symbol. This confused an encoding guesser. Don&#x27;t go there.<p>[1] <a href="http:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;html5&#x2F;syntax.html#determining-the-character-encoding" rel="nofollow">http:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;html5&#x2F;syntax.html#determining-the-chara...</a>
评论 #9789347 未加载
PeterisPalmost 10 years ago
We work with a lot of multilingual text, and for &quot;what to know about encodings and character sets&quot; we have a very simple answer to that - a guideline called &quot;use UTF8 or die&quot;.<p>It&#x27;s not suitable for absolutely everyone (e.g. if you have a lot of Asian text then you may want a different encoding), but for our use case every single deviation causes lot of headache, risks and unneccessary work in fixing garbage data.<p>In simplistic terms what we mean by this guideline:<p>* in your app, 100% all human text should be stored UTF8 only, no exceptions. If you need to deal with data in other encodings - other databases, old documents, whatever - make a wrapper&#x2F;interface that takes some resource ID and returns the data with everything properly converted to UTF8; and has no way (or at least no convenient way) to access the original bytes in another encoding.<p>* in all persistence layers, store text only as UTF8. If at all possible, don&#x27;t even provide options to export files in other encodings. If legacy&#x2F;foreign data stores need another encoding, then in your code never have an API that requires a programmer to pass data in <i>that</i> encoding - the functions &quot;store_document_to_the_mainframe_in_EBCDIC&quot; and &quot;send_cardholder_data_to_that_weird_CC_gateway&quot; should take UTF8 strings only and handle the encodings themselves.<p>* in all [semi-]public API, treat text as UTF8-only and <i>document that</i>. If your API documentation mentions a text field, state the encoding so that there is no guessing or assuming by anyone.<p>* in all system configuration, set UTF8 as the default whenever possible. A database server? Make sure that any new databases&#x2F;tables&#x2F;text fields will have UTF8 set as the default, so unless someone takes explicit action then user-local-language encodings won&#x27;t accidentally appear.<p>* Whoever introduces a single byte of different encoding data is responsible for fixing the resulting mess. This is the key part. Did you write a data input function that passed on data in the user computer default encoding; tested it only on US-ASCII nonenglish symbols; and got a bunch of garbage data stored? You&#x27;re responsible for finding the relevant entries and fixing them, not only your code. Used a third party library that crashes or loses data when passed non-english unicode symbols? Either fix the library (if it&#x27;s OS) or rewrite code to use a different one.
peapickeralmost 10 years ago
From the article: &quot;Overall, Unicode is yet another encoding scheme.&quot;<p>It is more than that - for instance, it includes algorithms as well... for instance, dealing with RTL languages with ordering and shaping rules (i.e. Arabic), how to know what to do when RTL languages are mixed with LTR (is that &#x27;.&#x27; at the end of &#x27;123&#x27; a decimal point, or a period? (determines if it goes to the right or the left or the sequence)) and how to know when data is equivalent despite being normalized or not, etc...
scottfralmost 10 years ago
The linked article by Joel Spolksy is also great:<p><a href="http:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;articles&#x2F;Unicode.html" rel="nofollow">http:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;articles&#x2F;Unicode.html</a>
评论 #9789215 未加载
ninjakeyboardalmost 10 years ago
I hate that I have to know this stuff. Working on implementing a spec today where handling character encoding is a requirement.
vorgalmost 10 years ago
&gt; <i>It basically defines a ginormous table of 1,114,112 code points that can be used for all sorts of letters and symbols. That&#x27;s plenty to encode all existing, pre-historian and future characters mankind knows about. There&#x27;s even an unofficial section for Klingon. Indeed, Unicode is big enough to allow for unofficial, private-use areas.</i><p>The private use areas only encode about 137,000 codepoints (U+e000 to U+f8ff &amp; U+f0000 to U+10ffff) and are running out quickly. Most of U+e000 to U+f8ff is used by many different private agreements, and some pseudo-public ones like the Conscript registry which encodes Klingon, linked to in the article. Conscript also uses a large chunk of plane F to encode the constructed script <i>Kinya</i>, i.e. the 3696 codepoints in U+f0000 to U+f0e6f, see <a href="http:&#x2F;&#x2F;www.kreativekorp.com&#x2F;ucsur&#x2F;charts&#x2F;PDF&#x2F;UF0000.pdf" rel="nofollow">http:&#x2F;&#x2F;www.kreativekorp.com&#x2F;ucsur&#x2F;charts&#x2F;PDF&#x2F;UF0000.pdf</a> . It takes up so much room because it&#x27;s a block script like Korean Hangul and is encoded by formula just like Hangul. Each Korean Hangul block is made up of 2 or 3 jamo: one of 19 leading consonants, one of 21 vowels, and optionally one of 27 trailing consonants, giving a total of 19 * 21 * 28 = 11,172 possible syllable blocks, generated by formula into range U+ac00 to U+d7a3. Kinya also uses such a formula to generate its script, and I&#x27;m sure many other constructed block scripts will make their way into the quasi-official Conscript Registry. I&#x27;m even working on one of my own.<p>In fact, rather than filling up U+f0000 to U+10ffff, such conscripts only need to fill up the first quarter of it (i.e. U+f0000 to U+f7fff) for Unicode to run out of private use space, because the remainder (U+f8000 to U+10ffff) is needed for a second-tier surrogate system (see <a href="https:&#x2F;&#x2F;github.com&#x2F;gavingroovygrover&#x2F;utf88" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gavingroovygrover&#x2F;utf88</a> ) to extend the codepoint space back up to 1 billionish codepoints as it was originally specified by Pike and Thompson until it was clipped back down to 1 million in 2003.<p>So Unicode is not &quot;plenty to encode&quot; or &quot;big enough to allow for&quot; all known, future, or private-use characters.
评论 #9789938 未加载
keedotalmost 10 years ago
Interesting that I actually don&#x27;t need to know this stuff. I think you&#x27;ll find that MOST developers actually don&#x27;t need to know this stuff. People seem to forget that the vast bulk of developers are for corporate and in house development, single language, being English.<p>I know this stuff because I like to understand how this works, but for all the dev&#x27;s under me, there are probably a thousand concepts that I want them to understand before they start tackling encoding beyond knowing when to call the correct function.
评论 #9789301 未加载
评论 #9789461 未加载
评论 #9789369 未加载
评论 #9789914 未加载
imaokalmost 10 years ago
One thing I&#x27;m still confused about. What exactly is happening when you copy paste some text from one app to another? What encoding will the copied text be in?
JoachimSalmost 10 years ago
Good, gentle introduction that goes through everything step by step. Turns to php at the end.
carsonreinkealmost 10 years ago
...never assume one byte per glyph
SilasXalmost 10 years ago
Sorry, but now I reflexively flag-on-sight any instance if this clickbaity, obviously overstated &quot;every programmer needs to know about semiconductor opcodes&#x2F;mainframe architecture&#x2F;etc&quot;.
SFjulie1almost 10 years ago
PHP devs are so slow they just adopted utf8 and see its glory.<p>I myself &quot;UTF8 or die!&quot;d a long time ago and discovered it was not a good idea.<p>I will forget the problem of the parsing of the nth character, the string length vs the memory used, the canonization of strings for comparison. And go directly to 2 problems:<p>* There exists cases in which latin1 &amp; utf8 are mangled in a same string. (ex http&#x2F;SIP headers are latin1 and content can be utf8 and you may want to store a full HTTP&#x2F;SIP transaction verbatim for debugging purpose), and it can store in iso-latin3 (code table for esperanto to be sarcastic), but will explode in utf8 unless you rencode it (B64)<p>* tools are partly UTF8 compliant: mysql (which is as good as PHP in terms of quality) is clueless about UTF8 (hint index and collation), and PHP too <a href="https:&#x2F;&#x2F;bugs.php.net&#x2F;bug.php?id=18556" rel="nofollow">https:&#x2F;&#x2F;bugs.php.net&#x2F;bug.php?id=18556</a> &lt;--- THIS BUG TOOK 10 YEARS TO BE CLOSED<p>The whole point is developer don&#x27;t understand the organic nature of culture, and especially of its writing and the diversity of culture.<p>They think that because some rules applies in their language it also applies in others: BUT<p>* PHP devs: lowercase of I is not always i (it can be i without a dot). It took 10 years to the dev to find where their bug was! * shortening a memory representation does not always shorten its graphical representation (apples bug with sms in arabian) * sort orders are not immutable (collation not only can vary from language to language but also according to the administrative context (ex: proper name in french)) * inflexions are hell and text size for error varies a lot (hence the unstability of windows 95 in french because error message where copied in a reserved page and the fixed size was less than the whole size of the domain&#x27;s corpus... hence any contiguous block in memory (lower xor upper bound) would have its memory potentially corrupted)).<p>My point is UTF8 is not hell. Real world is complex. And it becomes hell when some dumb devs thinking that by manipulating strings that represents any language they know about any language.. and apply universal rules that are not.<p>Some problems can be solved by ignoring them. But with culture it is not the case.<p>And actually, unicode SUX because it is US centrics<p>* computers should be able to store all our pasts books and make them available for the future, even in verbatim mode. But unicode HAS not archeological character sets like THIS <a href="https:&#x2F;&#x2F;fr.wikipedia.org&#x2F;wiki&#x2F;S_long" rel="nofollow">https:&#x2F;&#x2F;fr.wikipedia.org&#x2F;wiki&#x2F;S_long</a> I don&#x27;t care about the USA lack of history. I see no use in the computer if it requires to sacrifice our histories and cultures, * <a href="https:&#x2F;&#x2F;modelviewculture.com&#x2F;pieces&#x2F;i-can-text-you-a-pile-of-poo-but-i-cant-write-my-name" rel="nofollow">https:&#x2F;&#x2F;modelviewculture.com&#x2F;pieces&#x2F;i-can-text-you-a-pile-of...</a> some people cannot even use it in their own language<p>Unicode suffers a lot of problem plus a conceptual one; it is has immutable characters AND directives (change the direction of the writing, apply ligatures)... that not only will create security concerns (one of the funniest being the possibility by adding a string to reedit silently text in on an effector (screen or printer))... We are introducing type setting rules in unicode.<p>For those who have used tex since a long time, the non separation of the almost programmatical typography and the graphens is like not separing the model and the controller.<p>Which actually also calls for the view (the effection) and thus the fonts. Having the encoding of the Slong does not tell you what it looks like unless you have a canonical representation of the codepoint as a graphem.<p>And since we are printing&#x2F;creating document for juridical purpose we may like to control the view that ensures that the mapping of the string representation will not alter graphical representation in a way that can compromise its meaning. If someone signs in a box you don&#x27;t want the signature to alter the representation anywhere or worse without notice.<p>The devil lies in the detail. Unicode is a Babel tower that may well crash for the same reason as in the bible: hubris.