Conversion from gb2312 to Unicode in Erlang simplified Chinese characters (1)

Source: Internet
Author: User

These days, I have been pondering how to convert the simplified Chinese characters of Erlang to Unicode. I have thought of using external modules such as port, C, and python. Using ERTs, dict, and array is not only too cumbersome, but even hard to understand.

Two major issues to be considered in programming: Functions and efficiency.

Efficiency: not only the program running efficiency, but also the programming efficiency.

The simpler the code, the higher the efficiency. The use of simple tools and methods does not show the level of academic theory, so it is common to play profound.

Conversion from simplified Chinese characters to Unicode is not a profound problem. It can be found online in terms of theories and various solutions.

What is needed is to get inspiration from it and come up with the simplest method to apply in Erlang.

Okay, I found it.

1. Use the encoding conversion table in Erlang

There are two types of transcoding methods. One is to use an algorithm for random conversion, which saves time and space. The other is to find the previously created encoding table, which saves time and space.

From the perspective of practical requirements, the second method is better.

What should be the structure of this code table? This depends on the characteristics of simplified Chinese characters and Erlang strings.

Simplified Chinese character encoding. Here we only consider gb2312, which can meet many requirements. It is characterized by two non-ASCII characters, such as "medium" encoding d6d0.

The Erlang string is an integer list. For example, "medium" is [214,208], and Unicode is [20013].

Erlang has special internal methods for data query, and does not use array subscripts.

Based on these features, determine to use the built-in put/2 and get/1 Functions of Erlang, such:

Put ({214,208}, 20013) writes Unicode of the word "medium" to the memory;

Unicode = get ({214,208}) reads the Unicode of the "medium" character from the memory.

The following function get_unicode/1 obtains the Unicode of the entire string, for example, get_unicode ("Chinese Window"), returns [,].

Get_unicode ([], u)->

Lists: reverse (U );

Get_unicode ([A | T], u) when a <128->

Get_unicode (T, [A | u]);

Get_unicode ([a, B | Z], u)->

H = get ({a, B }),

Get_unicode (z, [H | u]).

This function can be used to process strings mixed with Chinese and Spanish characters, for example:

Get_unicode2 ("fg34 text in ASD 55 window 66 tt4", [])

Result: [97,115,100,200 21475,116,116, 52]

The following problem is to put all gb2312 Chinese characters and their corresponding Unicode into the Erlang process one by one.

This is the encoding conversion table used in Erlang. It cannot be programmed in Erlang because ERlC, the Erlang compiler, does not support Unicode.

The specific solution will be discussed later.

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.