I want to convert a string containing a binary number into an array of bytes. For that, I have this function:
private byte[] ToByteArray(string StringToConvert)
{
byte[] ByteArray = new byte[StringToConvert.Length/8];
byte temp;
for (int i = 0; i < StringToConvert.Length; i=i+8)
{
for (int j = i; j < 8; j++)
{
temp = Convert.ToByte(StringToConvert[j]);
ByteArray[i]=ByteArray[i] << temp;
}
}
return ByteArray;
}
I get an error that I can not convert byte to int(?) at
ByteArray[i]=ByteArray[i]<< temp;
What am I doing wrong?
ByteArray[i]=(byte)(ByteArray[i] << temp);for (int j = i; j < 8;looks suspicious.0and then they only get shifted around... End result: all zeroes.