首页 新闻 搜索 专区 学院

c#数据结构的问题

0
悬赏园豆:10 [已解决问题] 解决于 2016-12-04 21:31

请问我在用BitConvert.ToUInt16();方法时返回的数是怎么算的?

寻求帮助的编程小白的主页 寻求帮助的编程小白 | 初学一级 | 园豆:36
提问于:2016-11-15 10:34
< >
分享
最佳答案
0

这有啥好算的.touint16的意思是转成16位无符号整数.

跟16进制都没点卵关系.你看下他的参数是什么.如果是1个byte.那直接赋值都可以.根本不用运算.

收获园豆:10
吴瑞祥 | 高人七级 |园豆:29187 | 2016-11-15 11:27
其他回答(2)
0

按照16进制来计算的

悦光阴 | 园豆:2251 (老鸟四级) | 2016-11-15 10:45

能不能给一个具体的演算步骤呢

 

0

参考http://bbs.csdn.net/topics/391976162?page=1

 

反编译的结果

using System;
using System.Security;

namespace System
{
    [__DynamicallyInvokable]
    public static class BitConverter
    {
        [__DynamicallyInvokable]
        public static readonly bool IsLittleEndian = true;

        [__DynamicallyInvokable]
        public static byte[] GetBytes(bool value)
        {
            return new byte[]
            {
                value ? 1 : 0
            };
        }

        [__DynamicallyInvokable]
        public static byte[] GetBytes(char value)
        {
            return BitConverter.GetBytes((short)value);
        }

        [__DynamicallyInvokable, SecuritySafeCritical]
        public unsafe static byte[] GetBytes(short value)
        {
            byte[] array;
            byte[] expr_06 = array = new byte[2];
            byte* ptr;
            if (expr_06 == null || array.Length == 0)
            {
                ptr = null;
            }
            else
            {
                fixed (byte* ptr = &array[0])
                {
                }
            }
            *(short*)ptr = value;
            ptr = null;
            return expr_06;
        }

        [__DynamicallyInvokable, SecuritySafeCritical]
        public unsafe static byte[] GetBytes(int value)
        {
            byte[] array;
            byte[] expr_06 = array = new byte[4];
            byte* ptr;
            if (expr_06 == null || array.Length == 0)
            {
                ptr = null;
            }
            else
            {
                fixed (byte* ptr = &array[0])
                {
                }
            }
            *(int*)ptr = value;
            ptr = null;
            return expr_06;
        }

        [__DynamicallyInvokable, SecuritySafeCritical]
        public unsafe static byte[] GetBytes(long value)
        {
            byte[] array;
            byte[] expr_06 = array = new byte[8];
            byte* ptr;
            if (expr_06 == null || array.Length == 0)
            {
                ptr = null;
            }
            else
            {
                fixed (byte* ptr = &array[0])
                {
                }
            }
            *(long*)ptr = value;
            ptr = null;
            return expr_06;
        }

        [__DynamicallyInvokable, CLSCompliant(false)]
        public static byte[] GetBytes(ushort value)
        {
            return BitConverter.GetBytes((short)value);
        }

        [__DynamicallyInvokable, CLSCompliant(false)]
        public static byte[] GetBytes(uint value)
        {
            return BitConverter.GetBytes((int)value);
        }

        [__DynamicallyInvokable, CLSCompliant(false)]
        public static byte[] GetBytes(ulong value)
        {
            return BitConverter.GetBytes((long)value);
        }

        [__DynamicallyInvokable, SecuritySafeCritical]
        public unsafe static byte[] GetBytes(float value)
        {
            return BitConverter.GetBytes(*(int*)(&value));
        }

        [__DynamicallyInvokable, SecuritySafeCritical]
        public unsafe static byte[] GetBytes(double value)
        {
            return BitConverter.GetBytes(*(long*)(&value));
        }

        [__DynamicallyInvokable]
        public static char ToChar(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }
            if ((ulong)startIndex >= (ulong)((long)value.Length))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }
            if (startIndex > value.Length - 2)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            return (char)BitConverter.ToInt16(value, startIndex);
        }

        [__DynamicallyInvokable, SecuritySafeCritical]
        public unsafe static short ToInt16(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }
            if ((ulong)startIndex >= (ulong)((long)value.Length))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }
            if (startIndex > value.Length - 2)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            if (startIndex % 2 == 0)
            {
                return *(short*)(&value[startIndex]);
            }
            if (BitConverter.IsLittleEndian)
            {
                return (short)((int)(*(&value[startIndex])) | (int)(&value[startIndex])[1] << 8);
            }
            return (short)((int)(*(&value[startIndex])) << 8 | (int)(&value[startIndex])[1]);
        }

        [__DynamicallyInvokable, SecuritySafeCritical]
        public unsafe static int ToInt32(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }
            if ((ulong)startIndex >= (ulong)((long)value.Length))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }
            if (startIndex > value.Length - 4)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            if (startIndex % 4 == 0)
            {
                return *(int*)(&value[startIndex]);
            }
            if (BitConverter.IsLittleEndian)
            {
                return (int)(*(&value[startIndex])) | (int)(&value[startIndex])[1] << 8 | (int)(&value[startIndex])[2] << 16 | (int)(&value[startIndex])[3] << 24;
            }
            return (int)(*(&value[startIndex])) << 24 | (int)(&value[startIndex])[1] << 16 | (int)(&value[startIndex])[2] << 8 | (int)(&value[startIndex])[3];
        }

        [__DynamicallyInvokable, SecuritySafeCritical]
        public unsafe static long ToInt64(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }
            if ((ulong)startIndex >= (ulong)((long)value.Length))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }
            if (startIndex > value.Length - 8)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            if (startIndex % 8 == 0)
            {
                return *(long*)(&value[startIndex]);
            }
            if (BitConverter.IsLittleEndian)
            {
                ulong arg_7E_0 = (ulong)((int)(*(&value[startIndex])) | (int)(&value[startIndex])[1] << 8 | (int)(&value[startIndex])[2] << 16 | (int)(&value[startIndex])[3] << 24);
                int num = (int)(&value[startIndex])[4] | (int)(&value[startIndex])[5] << 8 | (int)(&value[startIndex])[6] << 16 | (int)(&value[startIndex])[7] << 24;
                return (long)(arg_7E_0 | (ulong)((ulong)((long)num) << 32));
            }
            int num2 = (int)(*(&value[startIndex])) << 24 | (int)(&value[startIndex])[1] << 16 | (int)(&value[startIndex])[2] << 8 | (int)(&value[startIndex])[3];
            return (long)((ulong)((int)(&value[startIndex])[4] << 24 | (int)(&value[startIndex])[5] << 16 | (int)(&value[startIndex])[6] << 8 | (int)(&value[startIndex])[7]) | (ulong)((ulong)((long)num2) << 32));
        }

        [__DynamicallyInvokable, CLSCompliant(false)]
        public static ushort ToUInt16(byte[] value, int startIndex)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
            }
            if ((ulong)startIndex >= (ulong)((long)value.Length))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }
            if (startIndex > value.Length - 2)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }
            return (ushort)BitConverter.ToInt16(value, startIndex);
        }

      
    }
}
View Code

 

 

 

hahanonym | 园豆:1460 (小虾三级) | 2016-11-15 11:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册