如何落后ASCII二进制code(C#) [英] How to get the binary code behind ASCII (C#)

查看:171
本文介绍了如何落后ASCII二进制code(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何输入转换到一个控制台,为二进制;怎么能这样的转换在C#中进行?结果
谢谢你在前进。


解决方案

 字符串s =到Console.ReadLine();
字节[]字节= Encoding.ASCII.GetBytes(S);

请注意,通过控制台使用的编码实际上不是ASCII ......你应该使用 Console.InputEncoding 而不是 Encoding.ASCII的

要获得每个字节的二进制重新presentation,您可以使用 Convert.ToString

 的foreach(以字节为单位字节​​B)
{
    Console.WriteLine(Convert.ToString(B,2));
}

I'm trying to find out how to convert input to a console, into binary; how can such a conversion be made in C#?
Thank you in advance.

解决方案

string s = Console.ReadLine();
byte[] bytes = Encoding.ASCII.GetBytes(s);

Note that the encoding used by the console isn't actually ASCII... you should probably use Console.InputEncoding instead of Encoding.ASCII

To get the binary representation of each byte, you can use Convert.ToString:

foreach(byte b in bytes)
{
    Console.WriteLine(Convert.ToString(b, 2));
}

这篇关于如何落后ASCII二进制code(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆