unicode的表示方式有两种,一种为web页面中使用的,一种为我们一般采用的编码方式
第一种:"成都 "Unicode编码方式 &+编号是网页里引用unicode字符的方法,编号为十进制的在unicode中的编号
第二种:\u6210\u90fd 表示的也是成都,采用的也是unicode编码格式,是java编程中使用的编码格式
它以\u开头,后接四位16进制的数。
以下是java中之间相互转化的代码
/*
* string与unicode之间相互转换
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class unicodeString {
public static void main(String[] args) {
String str = "中国";
System.out.println(unicodeString.StringToWebUnicode(str));
System.out.println(unicodeString.WebUnicodeToString("中国"));
System.out.println(unicodeString.StringToUnicode(str));
System.out.println(unicodeString.UnicodeToString("\u4e2d\u56fd\\uqqqq"));
}
/*
* 普通类型的unicode转string
*/
public static String UnicodeToString(String input) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(input);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
input = input.replace(matcher.group(1), ch + "");
}
return input;
}
/*
* string转普通类型的unicode
*/
public static String StringToUnicode(String input) {
String str = "";
for (char c : input.toCharArray()) {
if ((int) c > 128)
str += "\\u" + Integer.toHexString((int) c);
else
str += c;
}
return str;
}
/*
* string转web类型的unicode
*/
public static String StringToWebUnicode(String input) {
String str = "";
for (char c : input.toCharArray()) {
str += "&#" + (int) c + ";";
}
return str;
}
/*
* web类型的unicode转string
*/
public static String WebUnicodeToString(String input) {
String str = "";
String[] y1 = input.split(";");
for (String c : y1) {
if (c.length() > 2) {
str += (char) Integer.parseInt(c.substring(2));
}
}
return str;
}
}
js下将unicode转换为中文或字符串的代码
<script>
//带;号
var str="最新發https://www.jb51.net/article/1.htm";
//不带分号
var str2="https://www.jb51.net/article/1.htm";
function uncode(str) {
return str.replace(/&#(x)?([^&]{1,5});?/g, function (a, b, c) {
return String.fromCharCode(parseInt(c, b ? 16 : 10));
})
}
document.write(uncode(str));
document.write("<br>");
document.write(uncode(str2));
</script>
&#是什么编码
在网页中以&#开头的是HTML实体,一些字符在 HTML 中是预留的,拥有特殊的含义,比如小于号‘<’用于定义 HTML 标签的开始。如果我们希望浏览器正确地显示这些字符,我们必须在 HTML 源码中插入字符实体。
如何把汉字转换成HTML实体呢?
汉字的HTML实体由三部分组成,”&#+ASCII+;“ 即可。
例如,把“最新” 转换成“最新”
PHP函数把字符串或汉字转为HTML实体 htmlentities()
PHP函数把HTML实体转为字符串或汉字 html_entity_decode()
形如——
&#dddd;
&#xhhhh;
&#name;
——的一串字符是 HTML、XML 等 SGML 类语言的转义序列(escape sequence)。它们不是「编码」。
以 HTML 为例,这三种转义序列都称作 character reference:
前两种是 numeric character reference(NCR),数字取值为目标字符的 Unicode code point;以「&#」开头的后接十进制数字,以「&#x」开头的后接十六进制数字。
后一种是 character entity reference,后接预先定义的 entity 名称,而 entity 声明了自身指代的字符。
从 HTML 4 开始,NCR 以 Unicode 为准,与文档编码无关。
「中国」二字分别是 Unicode 字符 U+4E2D 和 U+56FD,十六进制表示的 code point 数值「4E2D」和「56FD」就是十进制的「20013」和「22269」。所以——
中国
中国
——这两种 NCR 写法都会在显示时转换为「中国」二字。
NCR 可以用于转义任何 Unicode 字符,而 character entity reference 很受限,参见 HTML 4 和 HTML5 中已有定义的字符列表:
- Character entity references in HTML 4
- Character entity references in HTML5
HtmlEncoder,中文转换成&#开头的编码(及HTML特殊字符解码)
package test.com.gjob.services;
import java.io.IOException;
import java.io.Writer;
public class HtmlEncoder {
public static void main(String args[]){
System.out.println(HtmlEncoder.encode("你好"));
}
/***
}
到此这篇关于&#是什么编码 unicode两种编码方式与中文的转换的文章就介绍到这了,更多相关unicode 编码内容请搜索自由互联以前的文章或继续浏览下面的相关文章,希望大家以后多多支持自由互联!
