

中文转换成html中的utf-8
source link: https://studygolang.com/articles/26515
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在HTML中,中文的“好好学习”可以表示为“好好学习”
在项目中,需要对接短信告警,短信告警返回数据要求是utf8的
后来继续沟通,才发现要的是 html-utf8 的;
没有找到合适的golang工具包,涉及语言转码的包主要有
mahonia,支持各种格式的数据转换,gbk,utf8,gb2312
net/html,支持网页转码,改的是转换网页中的<,>,&,',',.
根据网上经验,提供三个版本的转换:
Javascript
function ConvUtf8(obj) { returnobj.replace(/[^\u0000-\u00FF]/g,function($0) {returnescape($0).replace(/(%u)(\w{4})/gi, "$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(""+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
Recommend
-
48
Benefits for LWN subscribers The primary benefit fromsubscribing to LWN is helping to keep us publishing, but, beyond that, subscribers get immediate access to all site content a...
-
13
最近帮多个活跃的开源项目改了同一个 bug : 完善 Windows 下的 Unicode 支持。 问题源于 Windows 和其它平台不同,它有悠久的历史包袱。和现代大多数操作系统对 Unicode 支持的共识不同,它的 API 不是基于 UTF-8 而是基于...
-
29
When designing or, in some cases, implementing a programming language with built-in support for Unicode strings, an important decision must be made about how to represent or encode those strings in memory. Not all represe...
-
55
1.字符串 字符串在Go语言中以原生数据类型出现,使用字符串就像使用其他原生数据类型(int、bool、 float32、foat64等)一样。 字符串的值为双引号中的内容,可以在Go语言的源码中直接添加非ASCⅡ码字符 Go...
-
59
在电影《无间道》中,经常会出现摩斯密码的身影。摩斯密码本身的传奇性,为电影增色不少。其实摩斯密码一点也不复杂,反而很简单,透过摩斯密码,我们可以一窥计算机如何表示字符串的奥秘。
-
19
Over the past few weeks I've helped a new developer get started with both Mercurial and Rust , exposing them to somewhat niche subject...
-
26
Richard Suchenwirth 2001-02-28 - From a delightful debugging chat at theTcl chatroom, I was brought to write down what I think on UTF-8 analysis (cf.Unicode and UTF-8, see also). I imagine a UTF-8 string as a ra...
-
18
大多数的我们,真正认识到有字符编码这回事,一般都是因为遇到了乱码,因为我国常用的编码是 GBK 以及 GB2312:用两个 Byte 来表示所有的汉字,这样,我们一共可以表示 2^16 = 65536 个字符,一旦我们的 GBK 以及 GB2312 编码遇到了其他编...
-
3
本文记录一个小的算法,就是中文数字转换成数字算法 实现思路一: 1、从中文数字中逐个识别数字和权位组合 2、根据权位和数字倍数对应关系,计算出每个数字和权位组合值 3、最后求和得出结果 ...
-
7
PHP-中文转换成拼音 其中encode方法中传进两个参数,$utf8Data, $sRetFormat,第一个参数为传入的中文,字符编码为utf-8,如果不是这个编码要转换成utf-8,第二个参数head:首字母all:全拼音,返回值为转换后的拼音。 /**...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK