二进制,Int32和十六进制转换查询

时间:2016-03-14 17:27:14

标签: c# .net hex bit int32

下面的代码是一个正在进行的工作,我也花时间尝试学习更多关于位,十六进制和Int之间的转换; 很多事情显然都是重复性的操作,因为我们对7种不同的包装进行同样的操作,"所以请随意掩饰重复(只是希望整个代码结构可能提前回答一些问题)。

/*  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);

根据我迄今为止所学到的,如果二进制值转换为Int32,则第一个数字表示它是否为 - / +,其中1表示( - ),0表示(+);但是,在我的bitArrays中以0开头,当我执行CopyTo(int [])事务时,它们显示为负数,而以1开头的bitArrays在复制时显示为正数。

此外,还存在将它们从Int32值转换为Hex值的问题。从http://www.binaryhexconverter.com/检查时,数组转换中出现的任何值都不会将8 F添加到前面,因此我不确定其中的差异因为我的Hex知识是有限的,当我将数据传输到另一个系统时,我不想丢失有意义的数据(如果对任何人都很重要,则通过TCP / IP)。我会发布下面所有内容的值,以帮助清除它。

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

在线十六进制值:

FFFFFFFFAAAAAAAA
555555555
FFFFFFFFCCCCCCCC
33333333
FFFFFFFFC71C71C7
38E38E38
F0F0F0F

1 个答案:

答案 0 :(得分:0)

如果每个整数符号值反转,则放置-1 * theIntegerValue以使其反转。当你打电话给炒房时(" X"),可能还会有空白字符串?