<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lasana Murray</title>
    <description>The latest articles on DEV Community by Lasana Murray (@metasansana).</description>
    <link>https://dev.to/metasansana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F272990%2F393d8524-a87b-4d32-a6ed-68a755392e04.png</url>
      <title>DEV Community: Lasana Murray</title>
      <link>https://dev.to/metasansana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/metasansana"/>
    <language>en</language>
    <item>
      <title>Some Unicode Notes</title>
      <dc:creator>Lasana Murray</dc:creator>
      <pubDate>Sat, 16 Nov 2019 13:16:47 +0000</pubDate>
      <link>https://dev.to/metasansana/some-unicode-notes-905</link>
      <guid>https://dev.to/metasansana/some-unicode-notes-905</guid>
      <description>&lt;p&gt;Every now and again I find myself reading up on character sets, usually when I'm doing some kind of heavy text processing.&lt;/p&gt;

&lt;p&gt;When I first started out programming I used to tell myself, "Just use ASCII, it's not like I write programs for people who speak other languages right?".&lt;/p&gt;

&lt;p&gt;Well, as the Web became more multi-lingual, it's increasingly harder to justify that thinking.&lt;/p&gt;

&lt;p&gt;Even if you are not writing non-English programs there may be other reasons you want to pay attention to character encoding. UTF-8 is apparently mandatory on the Web so having a basic understanding could be seen as foundational.&lt;/p&gt;

&lt;p&gt;UTF-8 has some terms and notes I need to keep in mind:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* UTF-8 can use 1,2,3, or 4 bytes to encode a character, that is 8 bits, 16 bits, 24bits or 32bits,
* because of this it's called a variable width  character encoding.
* A UTF-8 file that contains only characters in the ASCII range is identical to an ASCII file.
* The term Basic Multilingual Plane(BMP) or Plane refers to the commonly used characters for all language scripts in the world.
* It contains the code points 0000- FFFF (hexadecimal).
* A breakdown of the groups can be found here.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Aside from UTF-8 there is also UTF-16 and UTF-32.&lt;/p&gt;

&lt;p&gt;Both are not backwards compatible with ASCII. That is, if you convert an ASCII file to UTF-16 or UTF-32,it's contents at the byte level are going to change (but not the actual data).&lt;/p&gt;

&lt;p&gt;UTF-16 is variable width like UTF-8 but uses either one or two 16 bit values for each code point. ASCII of course only uses one byte (8bits) for each of its characters, hence the backwards incompatibility.&lt;/p&gt;

&lt;p&gt;I should mention that a code point is the numeric value assigned to a character in a character set.Think of a character set as a huge array where the code point is an index.&lt;/p&gt;

&lt;p&gt;UTF-32 is fixed width and each character is encoded using 32bits (a whole 4 bytes!). Converting an ASCII file into UTF-16 or UTF-32 encoding is going to result in a larger size. That goes for databases as well.&lt;/p&gt;

&lt;p&gt;I think what trips me up are statements like "JavaScript/ECMAScript supports UTF-8" that I saw online when I was first learning the language. It had me assuming that the escape sequences \u0000 - \uFFFF are how you use UTF-8 code points in strings.&lt;/p&gt;

&lt;p&gt;I even wrote that here in an earlier draft.&lt;/p&gt;

&lt;p&gt;That's not really accurate, all strings in JavaScript are UTF-16 encoded, or at least, each code point is represented by a 16 bit value as the spec does not get into the details of implementation.&lt;/p&gt;

&lt;p&gt;When a code point in the range 0xD800 - 0xDBFF occurs next to a code point in the range 0xDC00 - 0xDFFF it is called a surrogate pair and the spec describes an algorithm for calculating the resulting value.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;\uXXXX&lt;/code&gt; syntax is actually for representing characters of the BMP that may not be on your keyboard. &lt;/p&gt;

&lt;p&gt;At runtime, it's still a 16 bit value in memory.&lt;/p&gt;

&lt;p&gt;Where does UTF-8 come in then? It's your actual source code! The JS files that you serve or pass to node are expected to be UTF-8 encoded!&lt;br&gt;
This is consistent with the requirement that everything be UTF-8 encoded. &lt;/p&gt;

&lt;p&gt;There may be an opportunities here for subtle bugs and even security issues but I have not taken the time to properly understand them yet. &lt;br&gt;
I do know that mixing up the encoding of your data in a MariaDB/MySQL server can cause your queries to give the wrong results.&lt;/p&gt;

&lt;p&gt;I have also heard of XSS and SQL injection bypasses by manipulating misconfigured character encoding. I'm starting to get a better understanding as to how, but again I have not dug into it yet.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
