本文介绍了将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?
推荐答案
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位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!