哪位有把\u2344\u5435\u9876这个转换为汉字的?
解决方案 »
- 怎样在sql server management studio express中导出表A的数据到某个文件?
- 如何在winform,c#循环取listView中的值呢?
- 请教COMBOX问题和窗体自动刷新问题
- 怎样利用反射获取一个类中的字段?
- 遍历树结点函数,老是报错,大侠们看看是哪里错了。
- 讨论C#下怎么做类似flashget的下载状态,先把我的贴出来
- 怎么做一个自己得控件?
- 有没有EF高手
- 请问Combobox如何能在窗口中显示时显示其中某一项(如items[1]中的内容),而非为空?
- 关于C#webbowser内部网页错位
- winform读xml的问题,一个排除异常的问题
- top 1 本来是得到一组一条记录是对的,但加了ORDER BY NEWID()后怎么会得到一组多条记录呢。看整句:
private string hexstr2str(string hexstring)
{
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
byte[] b = new byte[hexstring.Length / 2];
for (int i = 0; i < hexstring.Length / 2; i++)
{
b[i] = Convert.ToByte("0x" + hexstring.Substring(i * 2, 2), 16);
}
return encode.GetString(b);
} private string getultstr(string strcontent)
{
string aaa = strcontent;
int lenth = aaa.Length;
for (int i = lenth - 2; i >= 0; i -= 2)
{
aaa = aaa.Insert(i, "0x");
}
int z = aaa.Length / 4;
byte[] asdf = new byte[z];
for (int i = 0; i < z; i++)
{
asdf[i] = Convert.ToByte(aaa.Substring(0, 4), 16);
aaa = aaa.Substring(4);
}
return System.Text.Encoding.Default.GetString(asdf);
} public string ConvertTo_UTF8_String(string Msg)
{
byte[] buff = new byte[Msg.Length / 2];
string Message = "";
for (int i = 0; i < buff.Length; i++)
{
buff[i] = byte.Parse(Msg.Substring(i * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
Message = System.Text.UTF8Encoding.UTF8.GetString(buff);
return Message;
}
public static string GetChsFromHex(string hex)
{
if (hex == null)
throw new ArgumentNullException("hex");
if (hex.Length % 2 != 0)
throw new ArgumentException("hex is not a valid number!", "hex");
// 需要将 hex 转换成 byte 数组。
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
try
{
// 每两个字符是一个 byte。
bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
System.Globalization.NumberStyles.HexNumber);
}
catch
{
// Rethrow an exception with custom message.
throw new ArgumentException("hex is not a valid hex number!", "hex");
}
}
// 获得 GB2312,Chinese Simplified。
System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
return chs.GetString(bytes);
}
/// 将Unicode字串\u....\u....格式字串转换为原始字符串
/// </summary>
/// <param name="srcText"></param>
/// <returns></returns>
private string UnicodeToString(string srcText)
{
string dst = "";
string src = srcText;
int len = srcText.Length / 6;
for (int i = 0; i <= len - 1; i++)
{
string str = src.Substring(0, 6).Substring(2);
src = src.Substring(6);
byte[] bytes = new byte[2];
bytes[1] = byte.Parse(int.Parse(str.Substring(0, 2), System.Globalization.NumberStyles.HexNumber).ToString());
bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2), System.Globalization.NumberStyles.HexNumber).ToString());
dst += System.Text.Encoding.Unicode.GetString(bytes);
}
return dst;
}
调用:string src = @"\u2344\u5435\u9876";
string ret = UnicodeToString(str);
private string GetGB(string HexStr){
byte[] b = new byte[HexStr.Length/2];
for(int i=0;i<HexStr.Length;i+=2)
{
string str = Convert.ToInt32(HexStr.Substring(i,2),16).ToString();
b[i/2] = Convert.ToByte(HexStr.Substring(i,2),16);
}
return System.Text.Encoding.Default.GetString(b);
}
{
static void Main(string[] args)
{
String decodedString = DecodedString(@"\u2344\u5435\u9876");//将字节数组解码
Console.WriteLine("Decoded String:" + decodedString); //解码结果:?吵顶
Console.ReadLine();
} //单字符转换
private static ushort GetByte(char ch)
{
ushort rtnNum = 0;
switch (ch)
{
case 'a':
case 'A': rtnNum = 10; break;
case 'b':
case 'B': rtnNum = 11; break;
case 'c':
case 'C': rtnNum = 12; break;
case 'd':
case 'D': rtnNum = 13; break;
case 'e':
case 'E': rtnNum = 14; break;
case 'f':
case 'F': rtnNum = 15; break;
default: rtnNum = ushort.Parse(ch.ToString()); break; }
return rtnNum;
}
/// <summary>
/// 转换一个字符
/// </summary>
/// <param name="unicodeSingle"></param>
/// <returns></returns>
private static string ConvertSingle(string unicodeSingle)
{
if (unicodeSingle.Length!=4)
return null ;
byte[] unicodeBytes = new byte[2]{ 0, 0 };
for (int i = 0; i < 4; i++)
{
switch (i)
{
case 0: unicodeBytes[1] += (byte)(GetByte(unicodeSingle[i]) * 16); break;
case 1: unicodeBytes[1] += (byte)GetByte(unicodeSingle[i]); break;
case 2: unicodeBytes[0] += (byte)(GetByte(unicodeSingle[i]) * 16); break;
case 3: unicodeBytes[0] += (byte)GetByte(unicodeSingle[i]); break;
}
}
return Encoding.Unicode.GetString(unicodeBytes);
}
private static String DecodedString(String str)
{ String[] aStr = str.Split(new String []{ @"\u"},StringSplitOptions.RemoveEmptyEntries);
StringBuilder sb = new StringBuilder();
if (aStr.Length > 0)
{
for (int i = 0; i < aStr.Length; i++)
{
sb.Append(ConvertSingle(aStr[i]));
}
}
return sb.ToString();
}
}