在c#中将字符串转换为字节数组

时间:2021-04-03 19:48:30

I have a string,

我有一个字符串,

string Var="11001100" 

I want to convert it into a byte array.

我想将其转换为字节数组。

bArray[0]=0x00;
bArray[1]=0x00;
bArray[2]=0x01;
bArray[3]=0x01;
bArray[4]=0x00;
bArray[5]=0x00;
bArray[6]=0x01;
bArray[7]=0x01;

Can anybody guide me in this? I tried the following code, but I get the data in ASCII. I do not want that.

任何人都可以指导我吗?我尝试了以下代码,但是我得到了ASCII数据。我不要那个。

bArray = Encoding.Default.GetBytes(var);

3 个解决方案

#1


6  

I suggest using Linq:

我建议使用Linq:

using System.Linq;

...

string Var = "11001100";

byte[] bArray = Var
  .Select(item => (byte) (item == '0' ? 1 : 0))
  .ToArray(); 

Test:

测试:

Console.WriteLine(string.Join(Environment.NewLine, bArray
  .Select((value, index) => $"bArray[{index}]=0x{value:X2};")));

Outcome:

结果:

bArray[0]=0x00;
bArray[1]=0x00;
bArray[2]=0x01;
bArray[3]=0x01;
bArray[4]=0x00;
bArray[5]=0x00;
bArray[6]=0x01;
bArray[7]=0x01;

#2


1  

but I get the data in ASCII. I do not want that.

但是我用ASCII获取数据。我不要那个。

Then you need the string representation of the chars. You get it using the ToString method. This would be the old scool way simply using a reversed for-loop:

然后你需要字符的字符串表示。你可以使用ToString方法获得它。这将是简单地使用反向for循环的旧scool方式:

string Var="11001100";

byte [] bArray = new byte[Var.Length];

int countForward = 0;
for (int i = Var.Length-1; i >= 0 ; i--)
{
    bArray[countForward] = Convert.ToByte(Var[i].ToString());
    countForward++;
}

#3


0  

This is my solution for your question:

这是我的问题解决方案:

string value = "11001100";
int numberOfBits = value.Length;
var valueAsByteArray = new byte[numberOfBits];

for (int i = 0; i < numberOfBits; i++)
{
    bytes[i] = ((byte)(value[i] - 0x30)) == 0 ? (byte)1 : (byte)0;
}

Edit: Forgot the inversion.

编辑:忘记反转。

#1


6  

I suggest using Linq:

我建议使用Linq:

using System.Linq;

...

string Var = "11001100";

byte[] bArray = Var
  .Select(item => (byte) (item == '0' ? 1 : 0))
  .ToArray(); 

Test:

测试:

Console.WriteLine(string.Join(Environment.NewLine, bArray
  .Select((value, index) => $"bArray[{index}]=0x{value:X2};")));

Outcome:

结果:

bArray[0]=0x00;
bArray[1]=0x00;
bArray[2]=0x01;
bArray[3]=0x01;
bArray[4]=0x00;
bArray[5]=0x00;
bArray[6]=0x01;
bArray[7]=0x01;

#2


1  

but I get the data in ASCII. I do not want that.

但是我用ASCII获取数据。我不要那个。

Then you need the string representation of the chars. You get it using the ToString method. This would be the old scool way simply using a reversed for-loop:

然后你需要字符的字符串表示。你可以使用ToString方法获得它。这将是简单地使用反向for循环的旧scool方式:

string Var="11001100";

byte [] bArray = new byte[Var.Length];

int countForward = 0;
for (int i = Var.Length-1; i >= 0 ; i--)
{
    bArray[countForward] = Convert.ToByte(Var[i].ToString());
    countForward++;
}

#3


0  

This is my solution for your question:

这是我的问题解决方案:

string value = "11001100";
int numberOfBits = value.Length;
var valueAsByteArray = new byte[numberOfBits];

for (int i = 0; i < numberOfBits; i++)
{
    bytes[i] = ((byte)(value[i] - 0x30)) == 0 ? (byte)1 : (byte)0;
}

Edit: Forgot the inversion.

编辑:忘记反转。