惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Recent Announcements
Recent Announcements
Vercel News
Vercel News
M
MIT News - Artificial intelligence
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
G
Google Developers Blog
罗磊的独立博客
爱范儿
爱范儿
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
月光博客
月光博客
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - ahui

解决在IIS中调用Microsoft Office Excel组件后进程无法正常退出的问题 Javascript实现scroll时固定html元素 [小技巧]提交数据并下载文件的实现方式 轻量级DbHelper同时支持多个不同的数据库 Emit实现简单的C# AOP框架 Reflector的替代方案 .NET2.0项目Ajax请求处理 EntityFramework4.1和ADO.NET插入操作性能比较 一道算法题(负数左边,正数右边)的最优答案 MVC+ExtJs之验证信息无缝结合 设置预览图片自适应(保持原比例) Entity Framework4.1实现动态多条件查询、分页和排序 C#读写socket的常用方式 异步接收Socket数据 Mysql备份代码 android下修改hosts文件 Android和.NET通用的AES算法 .NET和Android解压缩处理 Android轻量级JSON操作类
16进制字符串和Byte之间的互转
ahui · 2011-08-29 · via 博客园 - ahui
public static class ByteHexHelper
{
    private static char[] _buffedChars = null;
    private static byte[] _buffedBytes = null;

    static ByteHexHelper()
    {
        _buffedChars = new char[(byte.MaxValue - byte.MinValue + 1) * 2];
        int idx = 0;
        byte b = byte.MinValue;
        while (true)
        {
            string hexs = b.ToString("X2");
            _buffedChars[idx++] = hexs[0];
            _buffedChars[idx++] = hexs[1];

            if (b == byte.MaxValue) break;
            ++b;
        }

        _buffedBytes = new byte[0x67];
        idx = _buffedBytes.Length;
        while (--idx >= 0)
        {
            if ((0x30 <= idx) && (idx <= 0x39))
            {
                _buffedBytes[idx] = (byte)(idx - 0x30);
            }
            else
            {
                if ((0x61 <= idx) && (idx <= 0x66))
                {
                    _buffedBytes[idx] = (byte)((idx - 0x61) + 10);
                    continue;
                }
                if ((0x41 <= idx) && (idx <= 70))
                {
                    _buffedBytes[idx] = (byte)((idx - 0x41) + 10);
                }
            }
        }
    }

    /// <summary>
    /// 字节数组转16进制字符串
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static string ByteToHex(byte[] bytes)
    {
        if (bytes == null)
        {
            return null;
        }

        char[] result = new char[bytes.Length * 2];
        for (int i = 0; i < bytes.Length; ++i)
        {
            int startIndex = (bytes[i] - byte.MinValue) * 2;
            result[i * 2] = _buffedChars[startIndex];
            result[i * 2 + 1] = _buffedChars[startIndex + 1];
        }

        return new string(result);
    }

    /// <summary>
    /// 16进制字符串转字节数组
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static byte[] HexToByte(string str)
    {
        if (str == null || (str.Length & 1) == 1)
        {
            return null;
        }

        byte[] result = new byte[str.Length / 2];
        int charIndex = 0;
        int byteIndex = 0;
        int length = result.Length;
        while (--length >= 0)
        {
            int first = 0;
            int second = 0;
            try
            {
                first = _buffedBytes[str[charIndex++]];
                second = _buffedBytes[str[charIndex++]];
            }
            catch
            {
                return null;
            }
            result[byteIndex++] = (byte)((first << 4) + second);
        }
        return result;
    }
}

posted @ 2011-08-29 00:46  ahui  阅读(7073)  评论()    收藏  举报