How to convert Chinese into UTF-8 in HTML

How to convert Chinese into UTF-8 in HTML

In HTML, the Chinese phrase “學好好學” can be expressed as “學好好學”

In the project, SMS alarms need to be connected, and the data returned by SMS alarms must be in utf8

After further communication, I found out that what was needed was html-utf8;

No suitable golang toolkit was found. The packages involved in language transcoding are mainly

mahonia, supports data conversion in various formats, gbk, utf8, gb2312

net/html, supports web page transcoding, which changes the conversion of <,>,&,',',.

Based on online experience, three versions of conversion are provided:

Javascript

function ConvUtf8(obj) {
returnobj.replace(/[^\u0000-\u00FF]/g,function($0) {returnescape($0).replace(/(%u)(\w{4})/gi, "&#x$2;") });
}

JAVA

public static String UTF8_html_conv(String str){
StringBuffer stbPreemptionArg = new StringBuffer();
for(int i = 0;i<str.length();i++){
if (str.codePointAt(i) > 255){
stbPreemptionArg.append("&#x"+Integer.toString(str.charAt(i), 16)+";");
}else{
stbPreemptionArg.append(str.charAt(i));
}
}
return stbPreemptionArg.toString();
}

Golang

func CovertToHtml(src string) string{
    rs := []rune(src)
    htmlUtf8 := ""
    for _, r := range rs {
        rint := int(r)
        if rint < 128 {
            htmlUtf8 += string(r)
        } else {
            //utf8 = "\\u"+strconv.FormatInt(int64(rint), 16)
            htmlUtf8 += "&#x"+strconv.FormatInt(int64(rint), 16) + ";"
        }
    }
return htmlUtf8
}

Summarize

The above is the method that the editor introduced to you to convert Chinese into UTF-8 in HTML. I hope it will be helpful to you!

<<:  WeChat applet custom menu navigation to achieve staircase effect

>>:  A brief discussion on the font settings in web pages

Recommend

Detailed introduction to CSS priority knowledge

Before talking about CSS priority, we need to und...

A brief discussion on the VUE uni-app development environment

Table of contents 1. Through HBuilderX visual int...

Linux Jenkins configuration salve node implementation process diagram

Preface: Jenkins' Master-Slave distributed ar...

Design Tips: We think you will like it

<br />Looking at this title, you may find it...

HTML&CSS&JS compatibility tree (IE, Firefox, Chrome)

What is a tree in web design? Simply put, clicking...

Use CSS to easily implement some frequently appearing weird buttons

background In the group, some students will ask r...

Learn how to use the supervisor watchdog in 3 minutes

Software and hardware environment centos7.6.1810 ...

CSS stacking and z-index example code

Cascading and Cascading Levels HTML elements are ...

MySQL data duplicate checking and deduplication implementation statements

There is a table user, and the fields are id, nic...

Web page experience: planning and design

1. Clarify the design direction <br />First,...

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

Vue3.0 routing automatic import method example

1. Prerequisites We use the require.context metho...