/// <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. json和csv文件存储

    一. json 1:基本概念 1.1 Json和Javascript JSON, 全称JavaScript Object Notation,它通过对象和数组的组合来表示数据.在JavaScript中一 ...

  2. select子句排列顺序与聚集函数

    selcet   要返回的列或表达式 from   从中检索数据的表 where    行级过滤 group by 分组说明 having 组级过滤 order by  输出排列顺序   ASC正序排 ...

  3. 调用jdbc已经写成的方法----jdbc工具类抽取方式一

    package web09; /*获取连接和释放资源的方法 */ import java.sql.Connection; import java.sql.DriverManager; import j ...

  4. iOS11 & iPhone X 适配指南

    苹果WWDC开发者大会上,终于发布了大家期待已久的iOS 11,有些新特性功能确实出人意料.不过大的方面苹果貌似也就 AR 和 GM 机器学习了,9月13日凌晨1点,苹果开了新品发布会,相信大家都已经 ...

  5. linux之getenv putenv setenv和unsetenv详解

    1.getenv函数 头文件:#include<stdlib.h> 函数原型: char * getenv(const char* name); 函数说明:getenv()用来取得参数na ...

  6. 关于ajax学习

    一.Ajax是XMLHttpRequest对象,javascript,XML,CSS,DOM等多种技术的组合 1.XML :(可扩展的标记语言) 提供了用于描述结构化数据的格式,适用于不同应用间的数据 ...

  7. 论文分享NO.1(by_xiaojian)

    论文分享第一期-2019.03.14: 1. Non-local Neural Networks  2018 CVPR的论文 2. Self-Attention Generative Adversar ...

  8. centos 7 查看所有登录用户的操作历史

    2019-01-07 转自  https://www.cnblogs.com/kevingrace/p/7373146.html centos 7 查看所有登录用户的操作历史 在Linux系统的环境下 ...

  9. Mac下的浏览器类似Windows中Ctrl+F5的不请求缓存刷新页面的快捷键

    正常方式: [shitf]+[command]+[r] 如果改过快捷键的: [fn]+[shift]+[command]+[f]

  10. (热死你)Resin https ssl Linux 配置,实战可用

    (热死你)Resin https ssl Linux 配置,实战可用 一.配置resin 1.在resin服务器中创建目录keys文件和openssl.conf,格式内容如下: #先复制以下的内容: ...