首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

bits 数组操作时,左右的有关问题。

2012-09-18 
bits 数组操作时,左右的问题。。。C# codebyte[] myByte1 new byte[] { 0x04, 0x3f}//字节数组,byte为0-25

bits 数组操作时,左右的问题。。。

C# code
byte[] myByte1 = new byte[] { 0x04, 0x3f};//字节数组,byte为0-255的类型             BitArray myBit2 = new BitArray(myByte1);                        PrintValues(myBit2, 8);


打印时不是我想要的结果
 “
 0000 0100 0011 1111


 而是从倒过来了


0010 000 1111 1100“



 是什么原因呢? 怎么才能是想要的结果那样呢? 谢谢



C# code
public static void PrintValues(IEnumerable myList, int myWidth) //myWidth指定每行显示的个数        {            int i = myWidth;            string num="";            foreach (Object obj in myList)  //迭代一列数            {                if (i <= 0)                {                    i = myWidth;                }                i--;                num += obj.Equals(true) ? ((ulong)1).ToString() : ((ulong)0).ToString();                          }            MessageBox.Show(num);                   }


[解决办法]
C# code
        private string GetBitString(byte byteDate)        {            string result = string.Empty;            for (int i = 0; i < 8; i++)            {                byte tempDate = (byte)(byteDate & 0x80);                if (tempDate == 0x80)                {                    result += "1";                }                else                {                    result += "0";                }                byteDate <<= 1;            }            return result;        }
[解决办法]
C# code
byte[] myByte1 = new byte[] { 0x04, 0x3f, 0x34, 0xd5, 0x6f };PrintValues(myByte1, 6);        static void PrintValues(byte[] b, int width)        {            StringBuilder strbuilder = new StringBuilder(b.Length * sizeof(byte));            int len = 0;            for (int i = 0; i < b.Length; i++)            {                for (byte j = 8; j > 0; j--)                {                    strbuilder.Append((b[i] & (byte)Math.Pow(2, j - 1)) >> (j - 1));                    len++;                    if (len % width == 0)                        strbuilder.Append('\n');                }            }            MessageBox.Show(strbuilder.ToString());        } 

热点排行