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

A brief discussion on several ways to implement front-end JS sandbox

Table of contents Preface iframe implements sandb...

How to use Vue cache function

Table of contents Cache function in vue2 Transfor...

Solution to occasional crash of positioning background service on Linux

Problem Description In the recent background serv...

Analysis of the causes of accidents caused by Unicode signature BOM

Maybe you are using include files here, which is u...

Detailed explanation of common Docker commands

1. Help Command 1. View the current Docker versio...

Facebook's nearly perfect redesign of all Internet services

<br />Original source: http://www.a-xuan.cn/...

js implements mouse switching pictures (without timer)

This article example shares the specific code of ...

How to use vuex in Vue project

Table of contents What is Vuex? Vuex usage cycle ...

A brief discussion on MySQL count of rows

We are all familiar with the MySQL count() functi...

How to operate Docker and images

Find mirror We can search for images from the Doc...

Summary of bootstrap learning experience-css style design sharing

Due to the needs of the project, I plan to study ...

Several commonly used methods for centering CSS boxes (summary)

The first one: Using the CSS position property &l...

Two methods to implement Mysql remote connection configuration

Two methods to implement Mysql remote connection ...