logo 广告
Loading...
 导航 所在位置:论坛首页 -> ┈┋电脑网络┋┈ -> Asp/Asp.Net教程 -> 字符串和十六进制之间的转换方法
回复
标题:字符串和十六进制之间的转换方法收藏 编辑 删除 楼主 | 上一篇 下一篇
有时也很傻
头像
等级:大侠
权限:普通用户
积分:122
金钱:6529
声望:224
经验:224
发帖数:1674
注册:2007年10月26日
资料 短消息2008-8-13 11:06:30

1.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;
        }

签名

2008-8-13 11:06:30 顶部
有时也很傻
头像
等级:大侠
权限:普通用户
积分:122
金钱:6529
声望:224
经验:224
发帖数:1674
注册:2007年10月26日
  资料  消息 短消息编辑 删除 引用 第2楼

2.VB6中的代码
'*******************************************************************

 '<函数:Encode>
'作用:将字符串内容转化为16进制数据编码,其逆过程是Decode
'参数说明:
'strSource 需要转化的原始字符串
Public Function Encode(strEncode As String) As String
    Dim i As Long
    Dim chrTmp$
    Dim ByteLower$, ByteUpper$
    Dim strReturn$  '存储转换后的编码
   
    For i = 1 To Len(strEncode)
        chrTmp$ = Mid(strEncode, i, 1)
        ByteLower$ = Hex$(AscB(MidB$(chrTmp$, 1, 1)))
        If Len(ByteLower$) = 1 Then ByteLower$ = "0" & ByteLower$
        ByteUpper$ = Hex$(AscB(MidB$(chrTmp$, 2, 1)))
        If Len(ByteUpper$) = 1 Then ByteUpper$ = "0" & ByteUpper$
        strReturn$ = strReturn$ & ByteUpper$ & ByteLower$
    Next
   
    Encode = strReturn$
End Function
'</函数:Encode>


'*******************************************************************

'*******************************************************************

 '<函数:Decode>
'作用:将16进制数据编码转化为字符串,是Encode的逆过程
Public Function Decode(strDecode As String) As String
    Dim i As Long
    Dim strCode$ '存储转换后的编码
    Dim chrTmp$
   
    On Error GoTo ErrProc
   
    If Len(strDecode) Mod 4 <> 0 Then GoTo ErrProc
    For i = 1 To Len(strDecode) Step 4
        strCode = Mid$(strDecode, i, 4)
        chrTmp$ = ChrW("&H" & strCode)
        If chrTmp$ = "?" Then If strCode <> "003F" Then GoTo ErrProc
        Decode = Decode & chrTmp$
    Next
   
    Exit Function
ErrProc:
    Decode = strDecode
    DealwithEvents "不能解析的消息:" & strDecode
End Function
'</函数:Decode>


'*******************************************************************

签名
2008-8-13 11:06:45 顶部
第1页 共1页 共1个回复     <<    >>    
 快速回复
  • 支持UBB,HTML标签

  • 高级回复

  • 操作选项:评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
      首页 | 购买指南 | 虚拟主机 | 特色介绍 | 下载中心 | 支付方式
    Copyright 2004-2008 BBSGood.com Powered By: BBSGood.Speed Version 5.0
      咨询电话:0575-85513832、0575-85513825(传真)、7*24小时咨询服务:13606552007 不良信息举报中心 浙ICP备05029817号
      业务QQ:38958768、客服QQ1:415896239、客服QQ2:343896043、MSN:jccsxx@hotmail.com