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

推荐订阅源

K
Kaspersky official blog
罗磊的独立博客
F
Fortinet All Blogs
人人都是产品经理
人人都是产品经理
量子位
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
B
Blog RSS Feed
腾讯CDC
博客园_首页
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
博客园 - Franky
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LINUX DO - 热门话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
D
Docker
P
Privacy & Cybersecurity Law Blog
S
Securelist
V
V2EX
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
T
Tor Project blog
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog
AWS News Blog
AWS News Blog
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
Recent Announcements
Recent Announcements
Cloudbric
Cloudbric
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术

博客园 - 努力&快乐

springboot 手动开启事务及手动提交事务 js MD5包含中文串时加密结果与JAVA结果不一致的解决方案 mongodb配置、启动、备份 JavaScript通过递归合并JSON linux系统关闭指定服务的方式 Dart 语言简易教程系列 查看java内存情况命令 JAVA用QRCode生成二维码 安装redis时Newer version of jemalloc required错误解决 代码指标工具 spring mvc接收ajax提交的JSON数据,并反序列化为对象 .net运行时dll的查找路径顺序 WebStorm11 注册 MSSql使用SQL语句快速查看表对的就说明,及表字段描述及字段类型 多个App间传递数据 cordova混合开发:Android中native调用javascript cordova plugin数据传递概要 javascript不用new关键字创建对象示例 .Net事件管道详解图
二进制样式的字符串与byte数组互转函数示例
努力&快乐 · 2017-03-08 · via 博客园 - 努力&快乐

开发时用到的方法,记录下:

    /// <summary>
    /// 测试方法 
    /// </summary>
    private void TestFun()
    {
        Response.Write("=================<p>");

        var b = BinaryStr2ByteArray("10011010010");
        var str = ByteArray2BinaryStr(b);
        str = str.TrimStart('0');
        Response.Write(str);        
        Response.Write("<p>=================");
        Response.End();
    }

    /// <summary>
    /// 二进制样式的字符串转byte数组
    /// </summary>
    /// <param name="binaryStr">二进制样式的字符串</param>
    /// <returns></returns>
    private byte[] BinaryStr2ByteArray(string binaryStr)
    {
        if (string.IsNullOrEmpty(binaryStr)) binaryStr = string.Empty;

        List<byte> byte_List = new List<byte>();
        var strL = binaryStr.Length;
        if (strL == 0)
            byte_List.Add(0);
        else if (strL > 0 && strL <= 4)
            byte_List.Add(Convert.ToByte(binaryStr, 2));
        else
        {
            var tempStr = string.Empty;
            for (var i = strL; i > 0; i = i - 4)
            {
                if (i - 4 > 0)
                    tempStr = binaryStr.Substring(i - 4, 4);
                else
                    tempStr = binaryStr.Substring(0, i);
                byte_List.Add(Convert.ToByte(tempStr, 2));
            }
        }

        byte_List.Reverse();
        return byte_List.ToArray();
    }


    /// <summary>
    /// byte数组转二进制样式的字符串
    /// </summary>
    /// <param name="byteArr">byte数组</param>
    /// <returns></returns>
    private string ByteArray2BinaryStr(byte[] byteArr)
    {
        List<string> strList = new List<string>();
        var bl = byteArr.Length;
        if (bl == 0)
            return "";
        else
        {
            var tempStr = string.Empty;
            for (var i = 0; i < bl; i++)
            {
                tempStr = Convert.ToString(byteArr[i], 2);
                if (tempStr.Length < 4)
                    tempStr = tempStr.PadLeft(4, '0');
                strList.Add(tempStr);
            }
        }

        return string.Join("", strList.ToArray());
    }


}
}

posted on 2017-03-08 09:06  努力&快乐  阅读(6104)  评论()    收藏  举报