Alex Watts
Alex Watts

Reputation: 567

Binary, Int32, and Hex conversions inquiry

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

Upvotes: 0

Views: 139

Answers (1)

Bobby C. Robillard
Bobby C. Robillard

Reputation: 157

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?

Upvotes: 0

Related Questions