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

/// <summary>
/// <函数:Encode>
/// 作用:将字符串内容转化为16进制数据编码,其逆过程是Decode
/// 参数说明:
/// strEncode 需要转化的原始字符串
/// 转换的过程是直接把字符转换成Unicode字符,比如数字"3"-->0033,汉字"我"-->U+6211
/// 函数decode的过程是encode的逆过程.
/// </summary>
/// <param name="strEncode"></param>
/// <returns></returns>
public static string Encode(string strEncode)
{
    string strReturn = "";//  存储转换后的编码
    foreach (short shortx in strEncode.ToCharArray())
    {
        strReturn += shortx.ToString("X4");
    }
    return strReturn;
}


/// <summary>
/// <函数:Decode>
/// 作用:将16进制数据编码转化为字符串,是Encode的逆过程
/// </summary>
/// <param name="strDecode"></param>
/// <returns></returns>
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值