/// <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. day 12 课后作业

    # -*- coding: utf-8 -*-# @Time : 2019/1/4 20:49# @Author : Endless-cloud# @Site : # @File : day 12 课 ...

  2. linux上搭建nginx+php+mysql环境详细讲解

    1.mysql安装 #安装编译环境 yum install -y gcc gcc-c++ gcc-devel g++ g++-devel; yum install -y wget yum instal ...

  3. git配置教程

    一.配置ssh 1.检查本机是否有ssh key设置 如果没有则提示: No such file or directory 如果有则进入~/.ssh路径下(ls查看当前路径文件,rm删除所有文件) 2 ...

  4. [原创] PHP 使用Redis实现锁

    目录 锁实现的注意点 加锁 connect 与 pconnect 解锁 Redis 中使用 Lua 脚本的注意点 Redis集群分布式锁 RedLock 算法 锁实现的注意点 互斥: 任意时刻, 只能 ...

  5. windows下vim中文乱码处理

    现象:gvim安装后,打开中文utf-8编码的文件中文显示乱码 处理:1.启动gvim8.0,菜单 ”编辑“->"启动设定"在文件最开始处添加如下两行set fileenco ...

  6. 工具篇-大数据组件的一些快捷Shell操作

    一.Hbase 1. HBase shell窗口进入 执行hbase shell 2. HBase表的创建 # 语法:create <table>, {NAME => <fam ...

  7. FJWC2019 最短路

    题目描述 有一张无向图,开始的时候所有边权为1,所有点没有权值,现在给定一个整数k,表示可以将k个点的点权设置为1,求点0到n-1的最短路最长是多少 Solution 网络流好题[然而本蒟蒻还是不会] ...

  8. 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  9. smarty 教程 及 常用点

    1. 简单例子 有助回忆基本知识点 define("DIR",dirname(__FILE__)); require_once(DIR."/libs/Smarty.cla ...

  10. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...