Lawrence Chou
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # My many years confusion on Unicode & UTF-8 --- ## Format - We will start with **questions** - Not structured --- ## How to participate - Feel free to interupt and raise discussiona at anytime! - especially if you have little prior knowledge on Unicode (like me from last week) --- ## Q1: Unicode v.s. UTF-8? - charset v.s. encoding --- ## Unicode is a charset - Unicode Charater Set (UCS) is a **charset**, a mapping between **characters** and **code points** - Other charsets: ASCII | Grapheme (character) | R | 讚 | 👍 | ... | | --- | --- | --- | --- | --- | | Unicode Code Points | [U+0052](https://www.compart.com/en/unicode/U+0052) | [U+8B9A](https://www.compart.com/en/unicode/U+8B9A) | [U+1F44D](https://www.compart.com/en/unicode/U+1F44D) | ... | --- ![unicode.org](https://i.imgur.com/A4gPbgX.png) --- ## UTF-8 is a encoding - UTF = Unicode Transformation Format - UTF-8 is a **encoding**, the algorithm for converting **Code Points** to **Bytes** - Other encoding: UTF-32, BIG5, GB2312 ... --- ## Let's see UTF-32 first <small> | Grapheme (character) | R | 讚 | 👍 | | --- | --- | --- | --- | | Unicode Code Points | [U+0052](https://www.compart.com/en/unicode/U+0052) | [U+8B9A](https://www.compart.com/en/unicode/U+8B9A) | [U+1F44D](https://www.compart.com/en/unicode/U+1F44D) || | UTF-32 Encoding | `0x00000052` | `0x00008B9A` | `0x0001F44D` | </small> - What are some problem? --- ## UTF-8 is a (better) encoding <small> | Grapheme (character) | R | 讚 | 👍 | | --- | --- | --- | --- | | Unicode Code Points | [U+0052](https://www.compart.com/en/unicode/U+0052) | [U+8B9A](https://www.compart.com/en/unicode/U+8B9A) | [U+1F44D](https://www.compart.com/en/unicode/U+1F44D) | | UTF-32 Encoding | `0x00000052` | `0x00008B9A` | `0x0001F44D` | | UTF-8 encoding | `0x52` <br> `[82]` | `0xE8 0xAE 0x9A` <br> `[232, 174, 154]` | ` 0xF0 0x9F 0x91 0x8D` <br> `[240, 159, 145, 141]` | </small> --- ## UTF-8 is a (better) encoding - Variable length encoding - "R" takes up 1 byte v.s. "讚" takes up 3 bytes - Save space - Can't to jump charactor of arbitrary index - Same ordering as unicode --- ![UTF-8](https://upload.wikimedia.org/wikipedia/commons/3/38/UTF-8_Encoding_Scheme.png) --- ### Ruby demo ```txt Ruby讚👍🏽 ``` ```ruby! content = File.open('my-utf8-file.txt').read # => "Ruby讚👍🏽\n" # Why Ruby know how? Encoding.default_external # => #<Encoding:UTF-8> content.length # Count by charactors! # => 8 content.split('') # => ["R", "u", "b", "y", "讚", "👍", "🏽", "\n"] content.bytes # => [82, 117, 98, 121, 232, 174, 154, 240, 159, 145, 141, # 240, 159, 143, 189, 10] content.split('').map{|char| [char, char.bytes]}.to_h # => {"R"=>[82], # "u"=>[117], # "b"=>[98], # "y"=>[121], # "讚"=>[232, 174, 154], # "👍"=>[240, 159, 145, 141], # "🏽"=>[240, 159, 143, 189], # "\n"=>[10]} content.split('').map{|char| [char, char.bytes, char.bytes.map{|byte| byte.to_s(2)}]} # => # [["R", [82], ["1010010"]], # ["u", [117], ["1110101"]], # ["b", [98], ["1100010"]], # ["y", [121], ["1111001"]], # ["讚", [232, 174, 154], # ["11101000", "10101110", "10011010"]], # ["👍", [240, 159, 145, 141], # ["11110000", "10011111", "10010001", "10001101"]], # ["🏽", [240, 159, 143, 189], # ["11110000", "10011111", "10001111", "10111101"]], # ["\n", [10], ["1010"]]] # Look for the pattern: # - `0xxxxxxx` # - `1110xxxx 10xxxxxx 10xxxxxx` # - `11110xxx 10xxxxxx 10xxxxxx` ``` --- ## Q: What does "UTF-8 is backward compatible w/ ASCII" actually means? ~~A valid JavaScript program is a valid TypeScript program.~~ A valid ASCII byte string is a valid UTF-8 byte string. ```ruby! "Ruby".encode("ASCII").bytes # => [82, 117, 98, 121] "Ruby".encode("UTF-32").bytes # => [0, 0, 254, 255, 0, 0, 0, 82, 0, 0, 0, 117, 0, 0, 0, 98, 0, 0, 0, 121] "Ruby".encode("UTF-8").bytes # => [82, 117, 98, 121] "Ruby".encode("UTF-16").bytes # => [254, 255, 0, 82, 0, 117, 0, 98, 0, 121] ``` --- ## Q: Why the hard-coded `4E00-9FFF` for chinese? - Let's check out [Unicode Planes & BMP](https://zh.wikipedia.org/zh-tw/Unicode%E5%AD%97%E7%AC%A6%E5%B9%B3%E9%9D%A2%E6%98%A0%E5%B0%84#%E5%9F%BA%E6%9C%AC%E5%A4%9A%E6%96%87%E7%A7%8D%E5%B9%B3%E9%9D%A2) - Planes are just visualization for code points grouping ![unicode BMP](https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/Roadmap_to_Unicode_BMP.svg/1100px-Roadmap_to_Unicode_BMP.svg.png =30%x) - Caveats - it’s actually CJVK - missing some newer CJK --- ## Q: Why Mojibake / 亂碼 / Buchstabensalat? A encoded byte string may not contain its encoding. We must guess which encoding to use. > ![Harry Porter](https://unicodebook.readthedocs.io/_images/Letter_to_Russia_with_krakozyabry.jpg =80%x) > From [Programming with Unicode - 3.11 Mojibake](https://unicodebook.readthedocs.io/definitions.html#mojibake-1) --- ## Q: "Unicode" is a "encoding" options in Windows? ![Window save as unicode encoding](https://i.imgur.com/8kB8v7D.png =40%x) - UTF-8 means `UTF-8` - Unicode big endian means `UTF-16BE` - Unicode means `UTF-16LE` 😱 <!-- ## Q: Why is 新同文堂 awesome? ## Q: BOM and endiness Endieness - UTF-16 quicklook - BOM (byte order mark) - `0xFF 0xFE ...`: little endian - `0xFE 0xFF`: big endian - Gulliver's Travels - BOM in `UTF-8`!? - Why we don’t like UTF-16? - Conclusion - Caveats ## Q: Why `UTF-8` has no endieness problem? There is no ambiguiaty for how to interpret UTF-8 in terms of endieness. ref: https://superuser.com/a/1648865/954056 ## Q: [UTF-8] Why so many `10xxxxxx`? Can quickly pick up in the middle. ## Q: What is `surrogate`? --> --- # Reference <small> - [YouTube - Unicode, in friendly terms: ASCII, UTF-8, code points, character encodings, and more](https://www.youtube.com/watch?v=ut74oHojxqo) - Only 10 mins - Visual demo - [內核恐慌 Podcast](https://pan.icu/) & [字谈字暢 Podcast](https://www.thetype.com/typechat/) - [EP026:Kerning Panic·字谈字串(二)](https://www.thetype.com/typechat/ep-026/) - [Programming with Unicode](https://unicodebook.readthedocs.io) - A comprehensive but succint little book - [pjchender - 瞭解網頁中看不懂的編碼:Unicode 在 JavaScript 中的使用](https://pjchender.dev/webdev/guide-unicode/) - Wikipedia - [Unicode](https://zh.wikipedia.org/zh-tw/Unicode) - [UTF-8](https://zh.wikipedia.org/zh-tw/UTF-8) - [Unicode Plane](https://zh.wikipedia.org/zh-tw/Unicode%E5%AD%97%E7%AC%A6%E5%B9%B3%E9%9D%A2%E6%98%A0%E5%B0%84#%E5%9F%BA%E6%9C%AC%E5%A4%9A%E6%96%87%E7%A7%8D%E5%B9%B3%E9%9D%A2) - [Unicode、UTF-8、UTF-16,終於懂了](https://www.readfog.com/a/1638084002220969984) - A great quick refresher in the future </small> --- ## Bonus - If you like computing history and trivia: - ⭐[Command Line Hero](https://www.redhat.com/en/command-line-heroes/season-1) podcast by Red Hat - recording 👍 - storytelling 👍 <!-- - ⭐ How to implement a search engine in 150 lines of Python -->

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully