0

The following code is a work-in-progress that I am also taking time with to try and learn some more about converting between bits, hex, and Int; A lot of this is obviously repetitive operations since we're doing the same thing to 7 different "packages," so feel free to gloss over the repeats (just wanted to have entire code structure up to maybe answer some questions ahead of time).

/*  Pack bits into containers to send them as 32-bit (4 bytes) items */
            int finalBitPackage_1 = 0;
            int finalBitPackage_2 = 0;
            int finalBitPackage_3 = 0;
            int finalBitPackage_4 = 0;
            int finalBitPackage_5 = 0;
            int finalBitPackage_6 = 0;
            int finalBitPackage_7 = 0;

            var bitContainer_1 = new BitArray(32, false);
            var bitContainer_2 = new BitArray(32, false);
            var bitContainer_3 = new BitArray(32, false);
            var bitContainer_4 = new BitArray(32, false);
            var bitContainer_5 = new BitArray(32, false);
            var bitContainer_6 = new BitArray(32, false);
            var bitContainer_7 = new BitArray(32, false);

            string hexValue = String.Empty;

            ...

           *assign 32 bits (from bools) to every bitContainer[] here*

            ...

/*  Using this single 1-D array for all assignments works because as soon as we convert arrays, 
                                        we store the result; this way we never overwrite ourselves    */
            int[] data = new int[1];

            /*  Copy containers to a 1-dimensional array, then into an Int for transmission  */
            bitContainer_1.CopyTo(data, 0);
            hexValue = data[0].ToString("X");
            finalBitPackage_1 = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

            bitContainer_2.CopyTo(data, 0);
            hexValue = data[0].ToString("X");
            finalBitPackage_2 = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

            bitContainer_3.CopyTo(data, 0);
            hexValue = data[0].ToString("X");
            finalBitPackage_3 = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

            bitContainer_4.CopyTo(data, 0);
            hexValue = data[0].ToString("X");
            finalBitPackage_4 = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

            bitContainer_5.CopyTo(data, 0);
            hexValue = data[0].ToString("X");
            finalBitPackage_5 = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

            bitContainer_6.CopyTo(data, 0);
            hexValue = data[0].ToString("X");
            finalBitPackage_6 = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

            bitContainer_7.CopyTo(data, 0);
            hexValue = data[0].ToString("X");
            finalBitPackage_7 = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

From what I've learned so far, if a binary value is being converted to Int32, the first digit tells if it will be -/+, where 1 indicates (-) and 0 indicates (+); however, in my bitArrays that start with a 0, they show up as a negative number when I do the CopyTo(int[]) transaction, and the bitArrays that start with a 1 show up as a positive when they are copied.

In addition, there is the problem of converting them from their Int32 values into Hex values. Any values that come out of the array conversion as negative don't get the 8 F's added to the front as when checked by http://www.binaryhexconverter.com/, so I wasn't sure the difference in that since my Hex knowledge is limited and I didn't want to lose meaningful data when I transmit the data to another system (over TCP/IP if it matters to anyone). I'll post the values I'm getting out of everything below to help clear it up some.


Variable                    Binary                     Int32[]       My Hex
bitContainer_1  "01010101010101010101010101010101"  "-1431655766"   AAAAAAAA
bitContainer_2  "10101010101010101010101010101010"  "1431655765"    55555555
bitContainer_3  "00110011001100110011001100110011"  "-858993460"    CCCCCCCC
bitContainer_4  "11001100110011001100110011001100"  "858993459"     33333333
bitContainer_5  "11100011100011100011100011100011"  "-954437177"    C71C71C7
bitContainer_6  "00011100011100011100011100011100"  "954437176"     38E38E38
bitContainer_7  "11110000111100001111000011110000"  "252645135"     F0F0F0F

Online Hex Values:

FFFFFFFFAAAAAAAA
555555555
FFFFFFFFCCCCCCCC
33333333
FFFFFFFFC71C71C7
38E38E38
F0F0F0F
Alex Watts
  • 537
  • 8
  • 27
  • 1
    The bits in a BitArray are in reverse. For instance, -1431655766 in binary is 10101010101010101010101010101010. – Dennis_E Mar 14 '16 at 17:44
  • On of my first thoughts when I was writing this post was that bitArray was being read [31]->[30]->[29]->etc as it's copied and because it was built [0]->[1]->[2]->etc via single, manual assignment it was causing it be interpreted backwards (the binary format posted in the OP is what bitArray reads as during breakpoints when I step through each variable at a time). So you think maybe change bitContainer_X.CopyTo() to something like bitContainer_X.CopyTo().Reverse()? – Alex Watts Mar 14 '16 at 17:49
  • FYI: they are all Int32. Their *base* is different but their *type* is the same. – Dennis_E Mar 14 '16 at 17:54
  • 1
    The F's are appearing in front because the site uses 64-bit numbers while you are using 32-bit numbers. You can see the difference with: `Convert.ToString(-1431655766, 16)` and `Convert.ToString(-1431655766L, 16)` – Dennis_E Mar 14 '16 at 18:02
  • You are not doing any conversions. You are "packing" bytes into numbers. – jdweng Mar 14 '16 at 18:05
  • @Dennis_E I had completely forgot Hex was 64 based! But perhaps this opens up a way for me to pack items into even fewer containers by doing Int64 instead of Int32!!! – Alex Watts Mar 14 '16 at 20:04
  • @jdweng I suppose you are correct. I was just under the impression that if an int array saw a list of 1's and 0's it would handle the binary to int implicitly – Alex Watts Mar 14 '16 at 20:05
  • 1
    There is no difference in the way C# stores integers and binary. The conversion to integers is performed in the input and output statements (reads & writes). You have to implicitly specify if the number is one, two, four, or 8 bytes. – jdweng Mar 14 '16 at 21:00

1 Answers1

0

If every integer sign value is reversed place a -1*theIntegerValue to un-reverse it. It could also have something to do with when you're calling toStirng("X"), maybe use a blank string?

  • So try just calling .ToString() with no parameter you think? I'll mention also that I'm fairly certain is was doing this before I had the .ToString() method added on there in an earlier version where I was just going binary to Int32 but not into Hex afterwards – Alex Watts Mar 14 '16 at 17:37
  • 1
    It's worth a shot because you're converting data to a string, the X that is also being converted could offset the code you want to be converted into Hex/Int32. If it was doing it before then I'm really not to sure, this is just my best guess as to a simple solution, let me know what happens. – Bobby C. Robillard Mar 14 '16 at 17:52
  • To be perfectly honest, I wasn't even sure how ToString("X") converts to hex, other than it's what other StackOverFlow posts said to use haha. I'll see how that works then, thanks! – Alex Watts Mar 14 '16 at 17:53