将表示二进制字符串的list <char>转换为ASCII C#

时间:2015-05-21 10:58:51

标签: c# list char ascii

我有一个表示二进制字符串的字符列表。

List<char> myCharList = new List<char>();

例如,charList保存由二进制序列表示的ascii H:01001000

我尝试将此List转换为ASCII,以便我可以在文本块中显示它。

谢谢

3 个答案:

答案 0 :(得分:4)

试试这个

string binary = "01001000";
string result =  Encoding.ASCII.GetString(binary.SplitByLength(8).Select(x => Convert.ToByte(x, 2)).ToArray());

<强>更新: SplitByLength:

public static IEnumerable<string> SplitByLength(this string str, int maxLength)
{
    for (int index = 0; index < str.Length; index += maxLength)
    {
        yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
    }
}

答案 1 :(得分:1)

没有linq的另一种方法

var LayerName = cc.Layer({})
var SceneName = cc.Scene.extend({
    onEnter: function() {
        this._super();
        var layer = new LayerName();
        this.addChild(layer);
    }
});

答案 2 :(得分:1)

这应该为您提供ASCII二进制字符串的字节表示:

static void Main(string[] args)
{

    List<char> chars = new List<char> {'A', 'E', 'L', 'L', 'O'};
    foreach (var c in chars)
    {
        string s = "";
        for (int i = 0; i < (sizeof(char) * 8); i++)
        {
            s = (1 & ((byte)c >> i)) + s;
        }
        Console.WriteLine("{0} represents {1}",c,s );
    }
    Console.Read();
}

这应该为您提供字节的ASCII表示。 请记住,在每个系统上,字符的大小可以不同,所以 此代码将使用char的系统默认大小:

/* Designed by Allan Wagner */

/* -font-family: 'Bitter', serif;- and -font-family: 'Montserrat', sans-serif;- */

*{margin:0;padding:0;}
body{margin:0;padding:0;background:#EEE;}
html, body{height:100%;}
.wrapper{min-height:100%;margin:0 auto -80px;} /* bottom margin is the negative value of the footers height */

/* Header Left*/
header{margin:0;padding:0;background:#FF4C4F;}
#header-content{margin:0 auto;padding:0;width:960px;overflow:auto;}
#header-left{margin:0;padding:0;float:left;}
#header-title{margin:10px 0;padding:0;font-family:"Montserrat", "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;font-size:28px;color:#EEE;text-decoration:none;}
#header-title:hover{color:#FFF;}

/* Header Right */
#header-right{margin:10px 0;padding:0;float:right;}
.searchbar-wrapper{margin:5px 10px;padding:0;float:left;width:440px;}
#searchbar{margin:0 auto;padding:4px;float:left;font-size:20px;border-radius:5px;outline:none;border:#999 thin solid;width:100%;}
#searchbar:hover{outline:none;border:#333 thin solid;}
#searchbar:active{outline:none;border:#000 thin solid;}
nav{margin:8px 10px 0;padding:0;float:right;}
.nav{margin:0 2px 0;padding:5px 10px;float:none;background:#BC2225;border-radius:5px;font-family:"Montserrat", "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;font-size:14px;color:#EEE;text-decoration:none;}
.nav:hover{color:#FFF;background:#A11C1E;}

/* Main Content */
#content-wrapper{margin:0 auto;padding:0;float:none;width:960px;height:auto;overflow:visible;}

/* Sidebar */
aside{margin:0;padding:0 10px 10px;background:#BBB;height:auto;width:270px;float:left;overflow:auto;}
.sidebarContainer{margin:10px 0 0 0;padding:0;width:100%;float:left;}
.ul-links{list-style:none;font-family:"Montserrat", "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;font-size:14px;font-weight:400;color:#222;text-decoration:none;}
.li-linklist{padding:0 7px 0;border:none;}
.li-linklist:hover{padding:0 6px 0;border-left:#000 thin solid;}
.li-links{margin:0;padding:0;text-decoration:none;color:#333;}
.li-links:hover{color:#000;}

section{margin:0;padding:0;}
.wSidebar{float:right;width:650px;padding:10px;background:#FFF;overflow:auto;}



/* Footer */
footer, .push{height:80px;clear:both;} /* push must be the same height as footer */
footer{margin:0;padding:20px 0 0;width:100%;background:#FF4C4F;}
.footer-wrapper{margin:0 auto;padding:0;width:960px;height:100%;}
#footer-left{margin:0;padding:0;float:left;}
#footer-right{margin:0;padding:0;float:right;}

/* Text Formatting */
h1{font-family:"Montserrat", "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;font-size:22px;}
h2{font-family:"Montserrat", "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;font-size:20px;font-weight:400;}
h3{font-family:"Montserrat", "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;font-size:16px;font-weight:400;}

p{font-family:"Bitter", Constantia, "Lucida Bright", "DejaVu Serif", Georgia, serif;font-size:16px;font-weight:400;}
.pstyle{font-family:"Bitter", Constantia, "Lucida Bright", "DejaVu Serif", Georgia, serif;font-size:16px;font-weight:400;}