UCS2编码转换C#
//字符“您”的UCS2 编码为:60A8(十六进制数字)
//编码
string ning = "您";
var chs = ning.ToCharArray();
int intVal = chs[0];
string s1 = intVal.ToString("X");
//转码
int num = Int32.Parse(s1, System.Globalization.NumberStyles.HexNumber);
string s2 = string.Format("{0}", (char)num);
//编码
string ning = "您";
var chs = ning.ToCharArray();
int intVal = chs[0];
string s1 = intVal.ToString("X");
//转码
int num = Int32.Parse(s1, System.Globalization.NumberStyles.HexNumber);
string s2 = string.Format("{0}", (char)num);
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace wangpu
{
public static class USC2Common
{
/// <summary>
/// UCS2解码
/// </summary>
/// <param name="src"> UCS2 源串 </param>
/// <returns> 解码后的UTF-16BE字符串</returns>
public static string DecodeUCS2(string src)
{
string decucs = src.Remove(src.IndexOf("\r"));
string pstr = "^[0-9a-fA-F]+$";
if (!Regex.IsMatch(decucs, pstr))
{
return "非法字符串无法解析!";
}
StringBuilder builer = new StringBuilder();
for (int i = 0; i < decucs.Length; i += 4)
{
int unicode_nu = Int32.Parse(decucs.Substring(i, 4), System.Globalization.NumberStyles.HexNumber);
builer.Append(string.Format("{0}", (char)unicode_nu));
}
return builer.ToString();
}
/// <summary>
/// UCS2编码
/// </summary>
/// <param name="src"> UTF-16BE编码的源串</param>
/// <returns>编码后的UCS2串 </returns>
public static string EncodeUCS2(string src)
{
StringBuilder builer = new StringBuilder();
builer.Append("000800");
byte[] tmpSmsText = Encoding.Unicode.GetBytes(src);
builer.Append(tmpSmsText.Length.ToString("X2")); //正文内容长度
for (int i = 0; i < tmpSmsText.Length; i += 2) //高低字节对调
{
builer.Append(tmpSmsText[i + 1].ToString("X2"));//("X2")转为16进制
builer.Append(tmpSmsText[i].ToString("X2"));
}
builer = builer.Remove(0, 8);
return builer.ToString();
}
}
}
using System.Text;
using System.Text.RegularExpressions;
namespace wangpu
{
public static class USC2Common
{
/// <summary>
/// UCS2解码
/// </summary>
/// <param name="src"> UCS2 源串 </param>
/// <returns> 解码后的UTF-16BE字符串</returns>
public static string DecodeUCS2(string src)
{
string decucs = src.Remove(src.IndexOf("\r"));
string pstr = "^[0-9a-fA-F]+$";
if (!Regex.IsMatch(decucs, pstr))
{
return "非法字符串无法解析!";
}
StringBuilder builer = new StringBuilder();
for (int i = 0; i < decucs.Length; i += 4)
{
int unicode_nu = Int32.Parse(decucs.Substring(i, 4), System.Globalization.NumberStyles.HexNumber);
builer.Append(string.Format("{0}", (char)unicode_nu));
}
return builer.ToString();
}
/// <summary>
/// UCS2编码
/// </summary>
/// <param name="src"> UTF-16BE编码的源串</param>
/// <returns>编码后的UCS2串 </returns>
public static string EncodeUCS2(string src)
{
StringBuilder builer = new StringBuilder();
builer.Append("000800");
byte[] tmpSmsText = Encoding.Unicode.GetBytes(src);
builer.Append(tmpSmsText.Length.ToString("X2")); //正文内容长度
for (int i = 0; i < tmpSmsText.Length; i += 2) //高低字节对调
{
builer.Append(tmpSmsText[i + 1].ToString("X2"));//("X2")转为16进制
builer.Append(tmpSmsText[i].ToString("X2"));
}
builer = builer.Remove(0, 8);
return builer.ToString();
}
}
}