我试图将布尔数组转换为一个字节 [英] I am trying to convert a boolean array to a byte
问题描述
< pre> // 我正在尝试转换布尔阵列(与8个值到一个字节
// 我也在尝试将转换的字节写入A FILE。
使用系统;
使用 System.IO;
使用 System.Collections;
命名空间 Applica
{
static class 计划
{
静态 void Main ( string [] args)
{
byte val;
bool tif = true ;
bool [] array = new bool [ 8 ];
const int BufferSize = 1024 ;
byte [] buffer = new byte < /跨度> [BUFFERSIZE];
FileInfo ap = new FileInfo( tempii .TXT跨度>);
string filePath = ap.FullName;
string destinationPath = Path.Combine( C:\\ check,Path.GetFileName(filePath));
使用(流输出= File.OpenWrite(destinationPath))
{
int bytes = 1 ;
while (bytes > 0 )
{
for ( int i = 0 ; i < 8 ; i ++)
{
if (tif == true )
{
tif = false ;
}
else if (tif == false )
{
tif = true ;
}
array [i] = tif;
if (i == 7 )
{
val = Convert.ToByte(array); // 无法将类型为'System.Boolean []'的对象转换为'System' .IConvertible'
}
}
output.Write(buffer, 0 ,bytes); // 我想写VAL来输出文件。
}
}
}
}
}
我尝试了什么:
我遇到了麻烦最后两行代码。我试图将具有8个值的布尔数组转换为一个字节。然后我试图将转换后的字节写入文件。
只需使用BitArray进行转换就可以废弃大量代码:
bool [] array = new bool [ 8 ];
array [ 1 ] = true ;
array [ 3 ] = true ;
array [ 6 ] = true ;
array [ 7 ] = true ;
BitArray bytes = new BitArray(array);
byte [] byteArray = new byte [ 1 ];
bytes.CopyTo(byteArray, 0 );
// byteArray [0]现在的值为0xCA
然后,您可以将字节数组写入文件,但它不会是文本,所以我不知道您为什么要使用文本文件。
您甚至可以跳过bool []数组,只需使用BitArray来保存所有bool值。
< pre lang =C#> BitArray bits = new BitArray( 8 ); // 保留8位
位[ 1 ] = true ;
位[ 3 ] = true ;
位[ 6 ] = true ;
位[ 7 ] = true ;
byte [] byteArray = new byte [ 1 ];
bits.CopyTo(byteArray, 0 );
<pre>// I AM TRYING CONVERT A BOOLEAN ARRAY (WITH 8 VALUES) TO A BYTE
// I AM ALSO TRYING TO WRITE THE CONVERTED BYTE TO A FILE.
using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
byte val;
bool tif = true;
bool[] array = new bool[8];
const int BufferSize = 1024;
byte[] buffer = new byte[BufferSize];
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int bytes = 1;
while (bytes > 0)
{
for (int i = 0; i < 8; i++)
{
if (tif == true)
{
tif = false;
}
else if (tif == false)
{
tif = true;
}
array[i] = tif;
if (i == 7)
{
val = Convert.ToByte(array);//Unable to cast object of type 'System.Boolean[]' to type 'System.IConvertible'
}
}
output.Write(buffer, 0, bytes);// I WANT TO WRITE "VAL" TO OUTPUT FILE.
}
}
}
}
}
What I have tried:
I am having trouble with last two lines of the code. I am trying to convert a boolean array with 8 values to a byte. Then I am trying to write the converted byte to a file.
You can scrap a lot of that code just by using a BitArray to do the conversion:
bool[] array = new bool[8]; array[1] = true; array[3] = true; array[6] = true; array[7] = true; BitArray bytes = new BitArray(array); byte[] byteArray = new byte[1]; bytes.CopyTo(byteArray, 0); // byteArray[0] now has the value 0xCA
You can then write the array of bytes to a file, though, it's not going to be text so I don't know why you're trying to use a text file.
You can even just skip the bool[] array and just use the BitArray to hold all the bool values.
BitArray bits = new BitArray(8); // Holds 8 bits bits[1] = true; bits[3] = true; bits[6] = true; bits[7] = true; byte[] byteArray = new byte[1]; bits.CopyTo(byteArray, 0);
这篇关于我试图将布尔数组转换为一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!