#是什么编码unicode两种编码⽅式与中⽂的转换
unicode的表⽰⽅式有两种,⼀种为web页⾯中使⽤的,⼀种为我们⼀般采⽤的编码⽅式
第⼀种:"成都 "Unicode编码⽅式 &+编号是⽹页⾥引⽤unicode字符的⽅法,编号为⼗进制的在unicode中的编号
第⼆种:\u6210\u90fd 表⽰的也是成都,采⽤的也是unicode编码格式,是java编程中使⽤的编码格式
它以\u开头,后接四位16进制的数。
以下是java中之间相互转化的代码
/*
* string与unicode之间相互转换
*/
import Matcher;
import 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 = Patternpile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(input);
char ch;
while (matcher.find()) {
ch = (char) Integer.up(2), 16);
input = up(1), ch + "");
}
return input;
}
/*
* string转普通类型的unicode
*/
public static String StringToUnicode(String input) {
String str = "";
for (char c : CharArray()) {
if ((int) c > 128)
str += "\\u" + HexString((int) c);
else
str += c;
}
return str;
}
/*
* string转web类型的unicode
*/
public static String StringToWebUnicode(String input) {
String str = "";
for (char c : CharArray()) {
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="最新發www.jb51/article/1.htm //不带分号
var str2="www.jb51/article/1.htm";
html document是什么function uncode(str) {
place(/&#(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.gjob.services;
import java.io.IOException;
import java.io.Writer;
public class HtmlEncoder {
public static void main(String args[]){
System.out.de("你好"));
}
/***
}
到此这篇关于&#是什么编码 unicode两种编码⽅式与中⽂的转换的⽂章就介绍到这了,更多相关unicode 编码内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章,希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。