0

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.

bArray = Encoding.Default.GetBytes(var);
9
  • 8
    Possible duplicate of Converting string to byte array in C# Commented Aug 18, 2017 at 8:30
  • Do you mean bit array instead? Commented Aug 18, 2017 at 8:32
  • I want byte array only.I tried that Encoding.ASCII.GetBytes(var); I do not want the data in acii i.e 48 for 0 and so on. I want to retain the binary string. Commented Aug 18, 2017 at 8:32
  • or you could do ascii arithmetics and subtract the ascii value of '0', which is 48 Commented Aug 18, 2017 at 8:32
  • 2
    what would be the outcome for this input: string Var="10101100"; ? Commented Aug 18, 2017 at 8:54

3 Answers 3

6

I suggest using 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;
Sign up to request clarification or add additional context in comments.

Comments

1

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

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:

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++;
}

6 Comments

Please notice, that bArray should contain inversed values: '1' -> 0 and '0' -> 1
@DmitryBychenko I have the feeling that OP is reading the input from right to left (little endian fashion). Therefore I chose the reversed for loop
I see; we need a better test example to distinguish between these two possibilities...
@DmitryBychenko lets see whether he answers my question from the comment
well, in case of, say, "11010101" the outcomes are different: [0, 0, 1, 0, 1, 0, 1, 0] (my understanding - bytes reversing) and [0, 1, 0, 1, 0, 1, 0, 0] (your reading - little/big endian). The test case provided "11001100" can't help to chose the right one (alas!)
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.