C# 如何转换八个数字的数组';0';和';1';c语言中的字符到字节#
我有一个字符数组,包括“0”和“1” 如何将其转换为c#中的字节数据类型 使用以下命令:C# 如何转换八个数字的数组';0';和';1';c语言中的字符到字节#,c#,C#,我有一个字符数组,包括“0”和“1” 如何将其转换为c#中的字节数据类型 使用以下命令: var bytes = Encoding.ASCII.GetBytes(chars); 您可以对其使用位运算符: char[] chars = new char[] {'1', '1', '1', '0', '0', '0' , '1', '1'}; byte theResult=0; foreach(char ch in chars) { byte bit=byte.Parse(ch.ToStr
var bytes = Encoding.ASCII.GetBytes(chars);
您可以对其使用位运算符:
char[] chars = new char[] {'1', '1', '1', '0', '0', '0' , '1', '1'};
byte theResult=0;
foreach(char ch in chars)
{
byte bit=byte.Parse(ch.ToString());
theResult=(byte)(theResult<<1);
theResult+=bit;
}
char[]chars=新字符[]{'1','1','1','0','0','1','1'};
字节结果=0;
foreach(char-ch-in-chars)
{
字节位=byte.Parse(ch.ToString());
theResult=(字节)(theResult让我们轻松开始
我们做一个循环,检查是否有一个'1'
,如果有,我们想把那个位添加到结果中
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = 0;
byte current = 1;
for (var index = 0; index < chars.Length; index++)
{
if (chars[index] == '1')
{
result += current;
}
current *= 2;
}
Console.WriteLine(result); // 198
检查一下,99
是1100011
二进制的。让我们把这个假设编成代码,好吗
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
if (chars.Length != 8)
{
throw new ArgumentOutOfRangeException();
}
byte result = 0;
byte current = 128;
for (var index = 0; index < 8; index++)
{
if (chars[index] == '1')
{
result += current;
}
current /= 2;
}
Console.WriteLine(result);
让我们进行二进制移位,而不是乘以2
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
if (chars.Length != 8)
{
throw new ArgumentOutOfRangeException();
}
byte result = 0;
byte current = 1;
for (var index = 7; index >= 0; index--)
{
if (chars[index] == '1')
{
result += current;
}
current <<= 1;
}
Console.WriteLine(result);
当然,如果可以通过其他方式保证,您可以删除验证。我假设您这样做了
接下来,让我们注意,当我们将char
强制转换为int
(这是一种安全的强制转换)时,'0'
将是48
,'1'
将是49
。这些值仅在最后一位(最小值)不同,该位是我们想要的值
知道了这一点,我可以直接设置位:
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = 0;
byte current = 1;
for (var index = 7; index >= 0; index--)
{
result |= unchecked((byte)(current * (chars[index] & 1)));
current <<= 1;
}
Console.WriteLine(result);
在上面的代码中,我没有保持当前状态
,而是在每次迭代中计算它
我们不需要乘法就可以这样做。我们将0
或1
移位
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = 0;
for (var index = 7; index >= 0; index--)
{
result |= unchecked((byte)(((chars[index] & 1) << (7 - index))));
}
Console.WriteLine(result);
我们可以稍微简化一下:
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = 0;
result |= unchecked((byte)(((chars[7] & 1) << 0)));
result |= unchecked((byte)(((chars[6] & 1) << 1)));
result |= unchecked((byte)(((chars[5] & 1) << 2)));
result |= unchecked((byte)(((chars[4] & 1) << 3)));
result |= unchecked((byte)(((chars[3] & 1) << 4)));
result |= unchecked((byte)(((chars[2] & 1) << 5)));
result |= unchecked((byte)(((chars[1] & 1) << 6)));
result |= unchecked((byte)(((chars[0] & 1) << 7)));
Console.WriteLine(result);
卓越!因此,我们从字符代码中提取0
或1
(使用掩码),并移动它在字节中的位置,把它放在一起,我们就得到了我们的结果。希望它的工作原理和实现方法都有意义。如果我们可以假设,如果输入数组中的字符不是'0'
,它将被视为'1'
,那么可以使用Linq来计算字节,如下所示:
byte b = chars
.Select((c, i) => (byte)(c == '0' ? 0 : 1 << (7-i))) // Convert each char to a power of 2.
.Aggregate((byte)0, (b1, b2) => (byte)(b1 | b2)); // OR all the converted values together.
字节b=chars
.选择((c,i)=>(byte)(c=='0'?0:1(byte)(b1 | b2));//或将所有转换的值一起选择。
请注意,移位是,您也可以使用Convert
进行移位
char[] chars = new char[] { '0', '1', '1', '0', '0', '0', '1', '1' };
string binaryString = string.Join(string.Empty, chars);
byte result = Convert.ToByte(binaryString, 2);
您的意思是将81
和0
转换为一个单字节吗?如果是这样,问题的结束时机还不成熟,问题应该更明确。如果是这样,可能会重新打开。@ehsanHB请详细说明。您希望的结果是什么?
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = 0;
for (var index = 7; index >= 0; index--)
{
result |= unchecked((byte)((1 << (7 - index)) * (chars[index] & 1)));
}
Console.WriteLine(result);
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = 0;
for (var index = 7; index >= 0; index--)
{
result |= unchecked((byte)(((chars[index] & 1) << (7 - index))));
}
Console.WriteLine(result);
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = 0;
result |= unchecked((byte)(((chars[7] & 1) << (7 - 7))));
result |= unchecked((byte)(((chars[6] & 1) << (7 - 6))));
result |= unchecked((byte)(((chars[5] & 1) << (7 - 5))));
result |= unchecked((byte)(((chars[4] & 1) << (7 - 4))));
result |= unchecked((byte)(((chars[3] & 1) << (7 - 3))));
result |= unchecked((byte)(((chars[2] & 1) << (7 - 2))));
result |= unchecked((byte)(((chars[1] & 1) << (7 - 1))));
result |= unchecked((byte)(((chars[0] & 1) << (7 - 0))));
Console.WriteLine(result);
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = 0;
result |= unchecked((byte)(((chars[7] & 1) << 0)));
result |= unchecked((byte)(((chars[6] & 1) << 1)));
result |= unchecked((byte)(((chars[5] & 1) << 2)));
result |= unchecked((byte)(((chars[4] & 1) << 3)));
result |= unchecked((byte)(((chars[3] & 1) << 4)));
result |= unchecked((byte)(((chars[2] & 1) << 5)));
result |= unchecked((byte)(((chars[1] & 1) << 6)));
result |= unchecked((byte)(((chars[0] & 1) << 7)));
Console.WriteLine(result);
char[] chars = new char[] {'0', '1', '1', '0', '0', '0' , '1', '1'};
byte result = unchecked((byte)(
((chars[7] & 1) << 0)
| ((chars[6] & 1) << 1)
| ((chars[5] & 1) << 2)
| ((chars[4] & 1) << 3)
| ((chars[3] & 1) << 4)
| ((chars[2] & 1) << 5)
| ((chars[1] & 1) << 6)
| ((chars[0] & 1) << 7)
));
Console.WriteLine(result);
byte b = chars
.Select((c, i) => (byte)(c == '0' ? 0 : 1 << (7-i))) // Convert each char to a power of 2.
.Aggregate((byte)0, (b1, b2) => (byte)(b1 | b2)); // OR all the converted values together.
byte b = 0;
for (int i = 0; i < 8; ++i)
if (chars[i] != '0')
b |= (byte)(1 << 7 - i);
char[] chars = new char[] { '0', '1', '1', '0', '0', '0', '1', '1' };
string binaryString = string.Join(string.Empty, chars);
byte result = Convert.ToByte(binaryString, 2);