/// <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. VS中工程的“依赖”,“库目录”,“包含目录”

    写多了Vs中的工程,就会遇到很多环境配置问题,例如“依赖项”,“库目录”,“包含目录”等等等等. 今天要记录的就是这些的基本含义:我们拿一个例子来看,更加清晰易懂一些: 例如在Opencv3.0+VS ...

  2. Flink学习笔记-新一代Flink计算引擎

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  3. P4382 [八省联考2018]劈配

    题目链接 题意分析 受到了\(olinr\ \ julao\)的影响 写了匈牙利算法 首先 我们对于每一个人 从高到低枚举志愿 如果当前志愿的老师有剩余的话 那么我们就选 否则的话 我们看看谁的那个志 ...

  4. Maven 依赖管理问题小计

    刚学Maven,遇到点小问题,记录一下.https://maven.apache.org/ 问题的起因是项目中使用了 Hibernate Validator ,但是运行起来后总是不能按照设置的注解校验 ...

  5. Centos7.4下安装Redis5.0

    一.下载Redis Redis下载地址:https://redis.io/download 二.安装依赖包 安装Redis之前需要安装c++命令 yum install gcc-c++ 三.上传并解压 ...

  6. Python3 操作系统与路径 模块(os / os.path / pathlib)

    #!/usr/bin/env python # coding=utf-8 __author__ = 'Luzhuo' __date__ = '2017/5/7' import os def os_de ...

  7. Vultr VPS建站攻略 – 一键安装宝塔面板架设LNMP/LAMP Web环境

    我们选择VULTR VPS建站的还是比较多的,其主要原因在于商家的稳定,毕竟我们用来建站选择服务器价格考虑的不是主要的(当然VULTR价格也是比较便宜),最为主要的是因为VULTR商家比较稳定,而且多 ...

  8. 接口测试-postman,JMeter与LoadRunner比较

    postman是一个谷歌出的轻量级的专门测试接口的小工具~(PS:postman包括两种:Chrome浏览器插件和postman客户端,我使用的是postman客户端)虽然不如firefox的post ...

  9. [HNOI2019]鱼

    Luogu5286 \(2019.4.14\),新生第一题,改了\(3\)个小时 题解-租酥雨,和出题人给的正解一模一样 枚举\(AD\),分别考虑鱼身\(BC\)和鱼尾\(EF\) 到\(E\),\ ...

  10. Office 的下载、安装和激活(图文详细)

    不多说,直接上干货! 在这里,推荐一个很好的网址,http://www.itellyou.cn/ 销售渠道不同,激活通道也不同.有零售版,有大客户版.零售的用零售密钥激活,一对一. SW开头或在中间有 ...