BartK
Posts: 4
Joined: Fri Oct 23, 2015 7:15 pm

BitArray conversion problem (.CopyTo missing)

Sun Oct 25, 2015 2:23 pm

I'm trying to convert a bool array to a 0x00 hex format to submit to an IO-expander (pcf8574).
While using the function below in another project (windows Form app) I'm able to process the code below with no problems but when I'm running to the windows device the array.CopyTo function is not usable and even not recognized anymore. (CS1501 No overload for method 'CopyTo' takes 2 arguments)
Is there any other way to get the correct byte or string to send over I2C to the pcf8574?

Code: Select all

bool[] BoolArray = new bool[] { true, false, false, true, true, false, true, true, };
            BitArray arr = new BitArray(BoolArray);
            byte[] data = new byte[1];
            arr.CopyTo(data, 0);
            string hex = BitConverter.ToString(data);
Please help me!

ricl
Posts: 657
Joined: Wed Aug 26, 2015 11:55 am

Re: BitArray conversion problem (.CopyTo missing)

Sun Oct 25, 2015 3:26 pm

Pretty much of the code examples from Microsoft use things like

Code: Select all

    byte[] WriteBuf_DataFormat = new byte[] { DATA_FORMAT, 0x01 };        
    byte[] WriteBuf_PowerControl = new byte[] { POWER_CONTROL, 0x08 };
    /* Write the register settings */
    try
    {
        I2CorSPI.Write(WriteBuf_DataFormat);
        I2CorSPI.Write(WriteBuf_PowerControl);
    }
Surely you must be able to set bits in those byte arrays?
ricl : F/gamma = ma : Law ii(a) : https://climatedatablog.wordpress.com/2016/01/02/an-energy-challenge-2016/ #AnEnergyChallenge2016

pwinwood
Posts: 76
Joined: Mon Jul 02, 2012 2:21 am
Location: Oxford, England

Re: BitArray conversion problem (.CopyTo missing)

Sun Oct 25, 2015 9:58 pm

I always use an enum with the [Flags] attribute to declare my bit values and then stuff them in a byte array as required.

ricl
Posts: 657
Joined: Wed Aug 26, 2015 11:55 am

Re: BitArray conversion problem (.CopyTo missing)

Mon Oct 26, 2015 12:06 am

Well if its missing then...

Code: Select all

private int getIntFromBitArray(BitArray bitArray)
{
    int value = 0;

    for (int i = 0; i < bitArray.Count; i++)
    {
        if (bitArray[i])
            value += Convert.ToInt16(Math.Pow(2, i));
    }

    return value;
}
from

http://stackoverflow.com/questions/5283 ... single-int
ricl : F/gamma = ma : Law ii(a) : https://climatedatablog.wordpress.com/2016/01/02/an-energy-challenge-2016/ #AnEnergyChallenge2016

BartK
Posts: 4
Joined: Fri Oct 23, 2015 7:15 pm

Re: BitArray conversion problem (.CopyTo missing)

Mon Oct 26, 2015 12:24 pm

But why is the copyTo method missing?

Array.CopyTo is available on:
.NET Framework 4.6 and 4.5
.NET Framework 4
Silverlight
.NET Framework 3.5
.NET Framework 3.0

The copyTo method seems to be the smallest and most easy way of getting the result I would like to get.
Thanks for the other solutions!

ricl
Posts: 657
Joined: Wed Aug 26, 2015 11:55 am

Re: BitArray conversion problem (.CopyTo missing)

Mon Oct 26, 2015 12:53 pm

Something to ask over in a Microsoft forum I suspect. Fact is, it didn't make it, so workarounds are all there are for now. (or find it in some other obscure place I suppose).
ricl : F/gamma = ma : Law ii(a) : https://climatedatablog.wordpress.com/2016/01/02/an-energy-challenge-2016/ #AnEnergyChallenge2016

Return to “Windows 10 for IoT”