/// <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. python中的字符串和编码

    了解编码之前首先说下这几个词的概率: 位.字节.字符.字符串 1.位(bit)也称为比特 这个其实很简单,因为计算机都是二进制存储数据,也就是0和1,一个0或者1就表示一位.这是计算机存储的最小单位. ...

  2. 考试题 T2

    题意分析 首先 要求起点终点不连通 再结合数据范围 就是最小割了 首先我们可以建一个图出来 如果\(x\)可以到\(y\)的话 那么我们就从\(x\)向\(y\)连一条代价为\(h[x]-h[y]+1 ...

  3. 【1】JMicro微服务-RPC体验

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl JMICRO运行简单服务提供者和消费者 1. 下载源代码 git checkout https://github.com/m ...

  4. Centos7下安装CUDA

    https://developer.nvidia.com/cuda-80-ga2-download-archive https://developer.nvidia.com/rdp/cudnn-dow ...

  5. opencv 将视频分解成图片和使用本地图片合成视频

    代码如下: // cvTest.cpp : Defines the entry point for the console application. #include "stdafx.h&q ...

  6. phpspreadsheet开发手记

    坑安装简单示例通过模板来生成文件释放内存单元格根据索引获取英文列设置值合并单元格居中显示宽度设置批量设置单元格格式直接输出下载自动计算列宽函数formula单元格变可点击的超链 PhpSpreadsh ...

  7. Mac 10.12高级防火墙pfctl教程收集(待实践)

    在Mac 10.10之前使用ipfw.之后升级为pfctl.由于Mac自带的GUI防火墙只能针对软件进行放开,且无法针对特定端口入站及出站进行管理.所以使用pfctl能解决这类问题. 收集教程如下: ...

  8. WCF系列教程之WCF操作协定

    一.简介 1.在定义服务协定时,在它的操作方法上都会加上OperationContract特性,此特性属于OperationContractAttribute 类,将OperationContract ...

  9. KNN理解

    基本思想 K近邻算法,即是给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的K个实例,这K个实例的多数属于某个类,就把该输入实例分类到这个类中.如下面的图: 通俗一点来说,就是找最 ...

  10. 深度学习(八) Batch Normalization

    一:BN的解释:  定义: 顾名思义,batch normalization嘛,就是“批规范化”咯.Google在ICML文中描述的非常清晰,即在每次SGD时,通过mini-batch来对相应的act ...