字符串和十六进制之间的转换方法

2008-08-13 10:26:24.0     推荐:0    收藏:0    评论:0     来源:中国IT实验

1.C#中的代码


///


/// <函数:Encode>
/// 作用:将字符串内容转化为16进制数据编码,其逆过程是Decode
/// 参数说明:
/// strEncode 需要转化的原始字符串
/// 转换的过程是直接把字符转换成Unicode字符,比如数字"3"-->0033,汉字"我"-->U+6211
/// 函数decode的过程是encode的逆过程.
///

///
///
public static string Encode(string strEncode)
{
string strReturn = "";// 存储转换后的编码
foreach (short shortx in strEncode.ToCharArray())
{
strReturn += shortx.ToString("X4");
}
return strReturn;
}
///
/// <函数:Decode>
///作用:将16进制数据编码转化为字符串,是Encode的逆过程
///

///
///
public static string Decode(string strDecode)
{
string sResult = "";
for (int i = 0; i < strDecode.Length / 4; i++)
{
sResult += (char)short.Parse(strDecode.Substring(i * 4, 4), global::System.Globalization.NumberStyles.HexNumber);
}
return sResult;
}

[第1页]   [第2页]   [下一页]
您可以针对本文进行:[评论]  [收藏]  [推荐]  
  • 共有0条评论  点击查看更多评论
  • 网友评论仅供网友表达个人看法,并不表明e800同意其观点或证实其描述
我想发表评论:
用户名密码
  • 匿名发表
    验证码: