/// <summary>
/// 中文转unicode
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string unicode_0(string str)
{
string outStr = "";
if (!string.IsNullOrEmpty(str))
{
for (int i = 0; i < str.Length; i++)
{
outStr += "/u" + ((int)str[i]).ToString("x");
}
}
return outStr;
}
/// <summary>
/// 汉字转为Unicode编码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string bgktounicode(string str)
{
string outstr = "";
//汉字转为Unicode编码:
string hz = str;
byte[] b = Encoding.Unicode.GetBytes(hz);
string o = "";
foreach (var x in b)
{
o += string.Format("{0:X2}", x) + " ";
}
outstr = o;
return outstr;
} /// <summary>
/// unicode转中文
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string unicode_1(string str)
{
string outStr = "";
if (!string.IsNullOrEmpty(str))
{
string[] strlist = str.Replace("/", "").Split('u');
try
{
for (int i = 1; i < strlist.Length; i++)
{
//将unicode字符转为10进制整数,然后转为char中文字符
outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
}
}
catch (FormatException ex)
{
outStr = ex.Message;
}
}
return outStr;
}
/// <summary>
/// unicode转中文(符合js规则的)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string unicode_js_1(string str)
{
string outStr = "";
Regex reg = new Regex(@"(?i)\\u([0-9a-f]{4})");
outStr = reg.Replace(str, delegate(Match m1)
{
return ((char)Convert.ToInt32(m1.Groups[1].Value, 16)).ToString();
});
return outStr;
}
/// <summary>
/// 中文转unicode(符合js规则的)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string unicode_js_0(string str)
{
string outStr = "";
string a = "";
if (!string.IsNullOrEmpty(str))
{
for (int i = 0; i < str.Length; i++)
{
if (Regex.IsMatch(str[i].ToString(), @"[\u4e00-\u9fa5]")) { outStr += "\\u" + ((int)str[i]).ToString("x"); }
else { outStr += str[i]; }
}
}
return outStr;
} /// <summary>
/// 骞垮憡涓戦椈
/// </summary>
/// <param name="utf8String"></param>
/// <returns></returns>
public static string unicodeTogbk(string utf8String)
{
string defaultString = "";
Encoding utf8 = Encoding.UTF8;
Encoding defaultCode = Encoding.Default;
// Convert the string into a byte[].
byte[] utf8Bytes = Encoding.Default.GetBytes(utf8String);
// Perform the conversion from one encoding to the other.
byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, utf8Bytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] defaultChars = new char[defaultCode.GetCharCount(defaultBytes, 0, defaultBytes.Length)];
defaultCode.GetChars(defaultBytes, 0, defaultBytes.Length, defaultChars, 0);
defaultString = new string(defaultChars);
return defaultString;
}
/// <summary>
/// 骞垮憡涓戦椈
/// </summary>
/// <param name="utf8String"></param>
/// <returns></returns>
public static string unicodeTogbkb(string utf8String)
{
string strBuffer = "";
byte[] buffer1 = Encoding.Default.GetBytes(utf8String);
byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length);
strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
return strBuffer;
}

csharp: string Encoding的更多相关文章

  1. Swift3 - String 字符串、Array 数组、Dictionary 字典的使用

    Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ...

  2. Type Encoding

    [Type Encodings] The compiler encodes the return and argument types for each method in a character s ...

  3. Java HttpURLConnection 抓取网页内容 解析gzip格式输入流数据并转换为String格式字符串

    最近GFW为了刷存在感,搞得大家是头晕眼花,修改hosts 几乎成了每日必备工作. 索性写了一个小程序,给办公室的同事们分享,其中有个内容 就是抓取网络上的hosts,废了一些周折. 我是在一个博客上 ...

  4. 系统变量file.encoding对Java的运行影响有多大?(转)good

    这个话题来自: Nutz的issue 361 在考虑这个issue时, 我一直倾向于使用系统变量file.encoding来改变JVM的默认编码. 今天,我想到, 这个系统变量,对JVM的影响到底有多 ...

  5. JAVA String 工具类

    java StringUtil 字符串工具类 import java.util.ArrayList; import java.util.LinkedHashSet; import java.util. ...

  6. Swift_字符串详解(String)

    Swift_字符串详解(String) 类型别名 //类型别名 fileprivate func testTypeAliases() { let index = String.Index.self p ...

  7. robotframework的学习笔记(十六)----robotframework标准库String

    官方文档:http://robotframework.org/robotframework/latest/libraries/String.html Introduction A test libra ...

  8. [Swift]LeetCode344. 反转字符串 | Reverse String

    Write a function that takes a string as input and returns the string reversed. Example 1: Input: &qu ...

  9. [Swift]扩展String类:Base64的编码和解码

    扩展方式1: extension String { //Base64编码 func encodBase64() -> String? { if let data = self.data(usin ...

随机推荐

  1. J2SE基本安装和java的环境变量

    J2SE基本安装和java的环境变量   1. 首先登录http://www.oracle.com,下载JDK(J2SE) JDK有很多版本其中JDK 1.0,1.1,1.2,1.3,1.4 1.5 ...

  2. 高斯分布(Gaussian Distribution)的概率密度函数(probability density function)

    高斯分布(Gaussian Distribution)的概率密度函数(probability density function) 对应于numpy中: numpy.random.normal(loc= ...

  3. 技巧方法 - CentOS6将Python2.6.6升级到Python2.7.6

    1.首先使用“python -V”命令查看python版本,我们测试主机显示的是2.6.6版,于是下面就着手将python2.6.6升级到Python2.7.6.python -V #查看python ...

  4. noip | 题目 | noip数据 收集站 | noipdata

    这是什么 一个NOIP历年比赛数据及题目的收集站,方便大家查找使用 网站链接:https://noipdata.github.io 点击这里立即跳转 新连接:noipdata.rcxzsc.com 点 ...

  5. USACO The Lazy Cow

    题目描述 这是一个炎热的夏天,奶牛贝茜感觉到相当的疲倦而且她也特别懒惰.她要在她的领域中找到一个合适的位置吃草,让她能吃到尽可能多的美味草并且尽量只在很短的距离.奶牛贝茜居住的领域是一个 N×N 的矩 ...

  6. vue项目微信回退按钮处理

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. Mac 10.12安装SVN工具SmartSVM 7.6

    说明:SVN工具没有最好的,只有用的最顺手的. 下载: (链接: https://pan.baidu.com/s/1dFGqEsT 密码: uyjx)

  8. Mac 10.12安装Google浏览器

    说明:先安装旧版本后续再升级,主要是资源难找. 下载: (链接: https://pan.baidu.com/s/1eROfQyY 密码: n6ij)

  9. Mac 10.12安装Go2Shell在资源管理器快速打开Shell并跳转到指定位置

    下载: (链接: https://pan.baidu.com/s/1qXAUqxY 密码: bf7g)

  10. Mac下的裁剪快捷键

    按[Command]+[C]复制文件,然后按[Command]+[Option]+[V]. 注意:[Command]+[X]只能剪切文字文本.