惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
V
V2EX
PCI Perspectives
PCI Perspectives
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 叶小钗
V
Visual Studio Blog
Jina AI
Jina AI
P
Proofpoint News Feed
罗磊的独立博客
SecWiki News
SecWiki News
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
Security Archives - TechRepublic
Security Archives - TechRepublic
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
N
News and Events Feed by Topic
NISL@THU
NISL@THU
T
Tailwind CSS Blog
T
Tenable Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tor Project blog
宝玉的分享
宝玉的分享
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
G
GRAHAM CLULEY

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN Mintty Tips and Configurations Generating Table of Contents for Markdown with Tagbar Convert Python Script to Exe on Windows with Pyinstaller Ubuntu on Windows Missing after Windows Update 使用代理加速 Mac 终端下载速度 My Experience with Several Zsh Plugin Managers 深圳租房小记 How to Install zplug inside Docker Container Why don't settings inside bashrc or bash_profile take effect? Setting Up Locale in Linux 谷歌 Adsense 申请及在 Hugo 中的配置 How to Write Algorithm Pseudo Code in LaTeX Nifty Nvim Techniques That Make My Life Easier -- Series 4 A Few Grammar Questions in Writing How to Read and Write Images with Unicode Paths in OpenCV Creating A Professional Table in LaTeX with booktabs How to Create Proper Folding for Vim/Nvim Configuration Linux Tips and Tricks -- s1 JPEG Image Orientation and Exif How Do I Show the Current File Path In Neovim? JPEG Image Quality in PIL Difference between view, reshape, transpose and permute in PyTorch Convert PIL or OpenCV Image to Bytes without Saving to Disk Fast Movement and Navigation Inside Vim or Neovim Unintuitive Behaviour of Case Sensitivity in Python glob Binding Keys in Zsh 几把机械键盘试用体验 Nvim Autocompletion with Deoplete Converting Markdown to Beautiful PDF with Pandoc Exclusive and Inclusive Motion in Neovim/Vim Nifty Nvim Techniques Which Make My Life Easier -- Series 3 Why Doesn't Jedi Autocompletion Work for Some Methods Vim-like Editing inside Browser Markdown 生成 HTML 时汉字之间出现多余空格问题 小米 9 安装谷歌商店(Google Play Store)与相关配置 Create Mappings That Take A Count in Neovim Spell Checking in Nvim English Words Completion inside Neovim/Vim How to Use Python Inside Vim Script with Neovim Nifty Little Nvim Techniques to Make My Life Easier -- Series 2 Setting up Ultisnips for Neovim Mac 上罗技 M590 鼠标设置 Nifty Little Nvim Techniques to Make My Life Easier -- Series 1 A Complete Guide on Writing LaTeX with Vimtex in Neovim Manipulating Images with Alpha Channels in Pillow Sublime Text Regular Expression Cheat Sheet Cropping Rotated Rectangles from Image with OpenCV Boosting Your Productivity on Terminal with Zsh and Plugins 最新版 Rime 输入法使用 (2022 更新) Display Image with Pillow inside Ubuntu on Windows Faster Directory Navigation with z.lua Cmder Advanced Configurations Nvim-qt Settings on Windows 10 Tmux Plugin Install and Management How to Debug Python Code in Terminal Markdown Writing and Previewing in Neovim -- A Complete Guide Line Number Settings for More Efficient Movement in Neovim 两个大规模中文语料库介绍以及处理 Windows 系统下几款程序员不可不用的神器 我的 2018 阅读清单 A Complete Guide to Neovim Configuration for Python Development How Is Newline Handled in Python and Various Editors? Two Issues Related to ImageFont Module in PIL 在 Listary 中调用 GoldenDict 或欧路词典查单词 Reading and Writing Text Files on Windows The Mathematics behind Font Shapes --- Bézier Curves and More 快速识别图片字体:字体识别工具介绍 Deoplete Failed to Load at Startup after Updating Python neovim Package What Is The Difference between pip, pip3 and pip3.6 Shipped with Anaconda3? Windows 10 系统下 Neovim 安装与配置
Character set, Encoding, Locale, Font and More...
2018-01-28 · via jdhao's digital space

It is almost destined that at some point of our programming years, we will meet the issue of character set, encoding and all the related stuff. There are tons of articles which explains the meaning of Unicode, UTF-8, UTF-16 etc. I am not going to repeat that here. In this post, I will write about what confused me when I tried to understand all this stuff.

We use characters every day. They are huge number of them, e.g., English characters (A, B, C), punctuations (,, ., !), Chinese characters (, ), Japanese characters (, ).

In order to represent all these characters or part of them, we give each character a unique number (you can think of this number as an ID). In other words, when we refer to a number, the corresponding character will be uniquely determined. We call this number code point.

Simply said, a character set is table which list the correspondence between code points and characters. One of the most simple character set is ASCII, in which characters are represented as code points in the range $[0, 127]$. For example, the code point of letter A is 65 (0x41 in hex format) and the code point of ! is 33 (0x21 in hex format). There are a lot of other character sets available, e.g., Unicode, GB18030, GBK, etc.

Now, let’s talk about encoding. Since we have character set, where each character gets its own code point. But how to represent these code points so that characters can be actually saved in the hard disk (encode issue), and how to read from the hard disk and know which character we are reading (decode issue)? That is what encodings deal with. With proper encoding of character set, we can save characters in disk and read characters from disk without any error. Since encoding is just a way to represent code points in memory, it is natural that various encodings exist for the same character set. Character set and encoding are different concept. Do not confuse them. Take Unicode for an example, Unicode is a character set, and several encodings such as UTF-8, UTF-16 and UTF-32 designed for it exist.

It should be noted that for simple character set such as ASCII and Latin 1, the code point of a character and its binary representation in memory are the same. That is because all the code point can be expressed in just one byte. That is why sometimes character set and encoding are use interchangeably. But for Unicode, the code point of a character does not necessarily equal to its binary representation due to encoding issues of several bytes.

References#

SBCS, DBCS and MBCS#

SBCS denotes single-byte character set, where all characters are represented using a single byte. Thus, the maximum characters which can be represented are 256. DBCS denotes double-byte character set, where all characters are represented using two bytes. It is easy to see that both SBCS and DBCS can represent only a limited number of characters. In contrast, a multi-byte character set (MBCS) can represent a character using single byte, two bytes, three bytes or even more bytes. This gives it much more flexibility to represent more characters. Table below lists some representative character sets,

character setrepresentatives
SBCSASCII, Latin 1
DBCSGB2312
MBCSUnicode

Unicode#

Unicode is a combined efforts from different organizations to try to standardize the representation of all existing characters in the world. It is the most comprehensive character set in the world, and it is updated regularly. Each character in Unicode is represented using a code point. The Unicode representation for code point starts with U+. For example, the code point for my name is U+90DD, U+6770 and U+4E1C. It is one of the most widely used character set in today’s world.

References#

Relationship between Unicode and UTF-8, UTF-16, UTF-32 etc.#

Unicode is just a comprehensive character set, which assign a code point to each character. In contrast, UTF-8, UTF-16 and UTF-32 are encodings which implement the Unicode. For example, UTF-8 decrees how the different characters in Unicode character set are actually represented in memory. In UTF-8 encoding, code point does not necessarily equal to the actual memory representation of a character. Take the character for an example, its code point is U+4E1C, and the byte representation in memory is E4B89C.

References#

GB2312, GBK, GB18030, Big5#

Like Unicode, both the above names denote different character sets related to Chinese (simplified or traditional).

GB2312 is an early character set for simplified Chinese, which contains 6763 Chinese characters (simplified) and 682 other signs. From WikiPedia,

GB2312 includes 6,763 Chinese characters (on two levels: the first is arranged by reading, the second by radical then number of strokes), along with symbols and punctuation, Japanese kana, the Greek and Cyrillic alphabets, Zhuyin, and a double-byte set of Pinyin letters with tone marks

Big5 is the character set for traditional Chinese characters used in Taiwan, HongKong etc. It is developed by 5 biggest IT companies in Taiwan in the 1980s.

GBK is an extended character set of the early GB2312 (k denotes Kuozhan in Chinese, which means extend). From WikipPedia,

GBK not only extended the old standard GB2312 with Traditional Chinese characters, but also with Chinese characters that were simplified after the establishment of GB2312 in 1981. With the arrival of GBK, certain names with characters formerly unrepresentable, like the “róng”(镕) character in former Chinese Premier Zhu Rongji’s name, are now representable.

GB18030 is a further improvement over GB2312 and GBK and is backward compatible with GB2312 and GBK. It encodes all the code points in the Unicode 3.0 standard. So we can convert between Unicode and GB18030.

References#

Microsoft Windows Code page#

In the old days before Unicode becomes popular. Microsoft use code pages to deal with different characters used by different regions in the world. The name code page has somewhat combined meanings. Sometimes, it refers to character sets, and other times, it means character encoding.

For computers sold in mainland China, the default code page for command line is code page 936 (or cp936 in short form). In the Windows command line, you can check the current code page by using chcp without any argument. If the error message from some executable are in Unicode format and you are using cp936, the error message will be shown as gibberish. The way to fix is to change your code page to UTF-8. The Windows code page for UTF-8 is 65001. You can use chcp 65001 or chcp utf-8 to change your code page to UTF-8.

References#

Character set and typeface (or font)#

Now that we have character set and encoding. How to display these characters in certain form (fontsize, bold or italic)? This is what the typeface or font (the two terms are often used interchangeably) do:

A font is a set of printable or displayable text characters in a specific style and size. The type design for a set of fonts is the typeface and variations of this design form the typeface family .

You may have already noticed that for a particular character, there are several font which can display it. See the image below for the display of the character in different fonts.

A font has many glyphs for different characters. But no font contains all the characters in the Unicode standard. According to discussion here, a maximum of 65536 glyphs are allowed for OpenType font. If you want to display a Unicode character in a certain font, you have to make sure the font has the glyph for that character. If the chosen font does not have a glyph for the character, the character may be displayed as a square box. See reference below on how to check if a character is included in a certain font.

References#

Similar character may have different code point#

The other day, I encounter a strange problem. Two characters looking similar are actually different. The two characters are shown below (enlarged in order to show their subtle difference):

Locale and difference between various UTF-8 locales#

In Linux system, we often see the term locale. What does that mean? According to oracle documentation:

A locale consists of a number of categories for which country-dependent formatting or other specifications exist. A program’s locale defines its code sets, date and time formatting conventions, monetary conventions, decimal formatting conventions, and collation (sort) order.

A locale name can be composed of a base language, country (territory) of use, and codeset. For example, German language is de, an abbreviation for Deutsch, while Swiss German is de_CH, CH being an abbreviation for Confederation Helvetica. This convention allows for specific differences by country, such as currency unit notation. In Oracle Solaris 11 the default locale codeset is UTF-8, an ASCII compatible 8-bit encoding form of Unicode. The fully defined locale name for Swiss German would thus be de_CH.UTF-8.

More than one locale can be associated with a particular language, which allows for regional differences. For example, an English-speaking user in the United States can select the en_US.UTF-8 locale (English for the United States), while an English-speaking user in Great Britain can select en_GB.UTF-8 (English for Great Britain).

Thus, different locales can share the same encoding UTF-8, which is why you see locales such as en_US.UTF-8 and zh_CN.UTF-8. In these locales, the characters use the same encoding UTF-8. Their differences reside in region-specific format, e.g., date format and monetary sign ($ or ).

References#

Search a Unicode character in Sublime Text using its code point#

We usually search a character using the character itself. But certain characters, such as the zero-width space character are hard to type. In that situation, it is easier to search such characters using their code points. In Sublime Text, you can use \x{CODE_POINT} to search a character with code point CODE_POINT. For example, for zero-width space, you can use \x{200b} to search it.

References#