将8位ASCII转换为6位 [英] Convert 8bit ASCII to 6bit

查看:596
本文介绍了将8位ASCII转换为6位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试获取一个char并将其转换为6位二进制文​​件.知道如何在C#中做到这一点.这是我到目前为止所拥有的.

Hi,

I am trying to take a char and convert it to 6 bit binary. Any idea how one can do this in C#. Here is what I have so far.

char c = '1';
int i = Convert.ToInt32(c);
while (i > 40)
    i -= 8;
Console.WriteLine(Convert.ToString(i, 2));
Console.ReadLine();



结果为"100001",应为"000001".

有什么想法吗?



The result for this is ''100001'', where it should be ''000001''.

Any ideas?

推荐答案

在您的示例中,ASCII字符"1"为十六进制的0x31或二进制的11 0001(已为6位).但是,字符"A"为0x41十六进制或二进制的100 0001,即7位.您希望如何准确地将其转换为6位而不丢失其定义?
In your example the ASCII character ''1'' is 0x31 hex or 11 0001 in binary which is already 6 bits. However the character ''A'' is 0x41 hex or 100 0001 in binary, which is 7 bits. How exactly do you expect to convert that to 6 bits without losing its definition?


尝试一下:

Try this:

private string ascii2ais(string p)
        {
            StringBuilder sb = new StringBuilder();

            char[] chars = p.ToCharArray();
            foreach (char c in chars)
            {
                int i = Convert.ToInt32(c);
                i -= 48;
                if (i>40)
                {
                    i -= 8;
                }

                string sixBit = Convert.ToString(i, 2).PadLeft(6, '0');
                sb.Append(sixBit + " ");
            }

            return sb.ToString();

        }



这应该可以解决问题.至少我尝试过将14eG转换为``000001 000100 101101 010111''



This should do the trick. At least I tried that 14eG is translated to ''000001 000100 101101 010111''


这篇关于将8位ASCII转换为6位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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