注册本站  论坛  繁體中文

电脑技巧
手机 | MP3 | MP4 | 显卡 | 主板 | 显示器 | 光存储 | 笔记本 | 网络设备 | 移动存储 | 数码相机
键鼠 | CPU | 音箱 | GPS | 电视 | 服务器 | 投影机 | 机箱电源 | 品牌电脑 | 办公打印 |
| 网站首页 | Cisco | Windows | Linux | Java | Dotnet | Oracle | 网页设计 | 平面设计 | 安全 | 软件应用 | 电脑维修 | 办公维修 |
您现在的位置: 电脑技巧 >> Dotnet >> C# >> Dotnet正文

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

文章来源:blog.csdn.net 作者:lianchao… 更新时间:2008-6-28 20:38:52 【 】 【加入收藏

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

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>

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

  • 上一篇Dotnet:

  • 下一篇Dotnet:
  • 最 新 热 门
     web.config配置文件中的 元素
     为网站添加业务层
     用SqlDataSource实现DataList嵌套DataList
     Visual Studio 2003插件的编写
     千条DOS命令收藏
     IIS 常见问题
     IIS需要的最小NTFS权限
     优化 .NET的性能
     设计模式与VB .net代码 外观模式,合成模式
     VB.net中介者模式
    最 新 推 荐
     Windows via C/C++ —进程(一)
     C#邮件发送程序
     扩展 ASP.NET 的客户端验证
     实现DataGridView中行的上下移动
     C#中的委托和事件
     与IDE相关的Attribute属性
     C#中using关键字的使用介绍
     C#FileStream复制大文件
     C#实用技巧:轻松实现对文件的操作
     C#实现所有经典排序算法
    相 关 文 章

    Excel导出时数据中有特殊字符的可能会出错
    ASP.NET 2.0中连接字符串的设置
    三种字符串加法运算的效率问题
    C#如何在RowHeader显示字符串和图标
    VB.NET字符数据类型和其他数据类型
    SQL关于特殊字符处理的基本方法
    JS中将字符串转为XML并读取对象值实例
    ASP获取字符串长度的自定义函数
    ASP使用的去掉字符串头尾连续回车和空格的函…
    ASP去掉字符串头尾连续回车和空格的Functio…

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告

     

    Copyright 2006-2008 pcjx.com All Rights Reserved
    电脑技巧 版权所有 粤ICP备06059145号 地图
    本网站所有内容未经许可不得转载或做其他使用