博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字节序(byte order)及其实现方法
阅读量:4071 次
发布时间:2019-05-25

本文共 3247 字,大约阅读时间需要 10 分钟。

字节序及其实现方法

一、字节序

我们一般把字节(byte)看作是数据的最小单位。当然,其实一个字节中还包含8个比特(bit)。字节序主要体现在字节之间的顺序,与位无关。

大端字节序 Big Endian(常见的有Motorola)

而所谓,便是指其“”落在低地址上的存储方式。例如像地址a写入0x0A0B0C0D之后,在内存中的数据便是:
小端字节序 Little Endian(常见的有Intel)
而对于来说就正好相反了,它把“”放在低地址上。例如:
二、实现方法(C#)
public static int GetSignalValueInBits(byte[] MsgArray,int byteNumber, int BitNum,int Length,string ByteOrder)        {            int nSigValueInBits = 0;            if (Length != 0 && MsgArray != null)            {                int nBytesToRead = 0;                /* 计算信号所包括的字节数 */                nBytesToRead = nGetNoOfBytesToRead(BitNum, Length);                /* 无论是哪种字节序,字节内顺序相同 */                int CurrBitNum = BitNum;                /* 若字节序是Motorola,则应该反向读取字节 */                int nByteOrder = (ByteOrder == "Intel(Litter Endian)") ? 1 : -1;                bool bValid = bValidateSignal(MsgArray.Count(), byteNumber, BitNum, Length, nByteOrder);                if (bValid == true)                {                    int nBitsRead = 0;                    for (int i = 0; i < nBytesToRead; i++)                    {                        /* 在消息字节数组中取字节的位置是byteNumber - 1 */                        byte byMsgByteVal = MsgArray[(byteNumber - 1) + (nByteOrder * i)];                        if (CurrBitNum != 0)                        {                            byMsgByteVal >>= CurrBitNum;                        }                        /* 计算当前字节中读取多少bits */                        int nCurrBitsToRead = Math.Min(8 - CurrBitNum, Length - nBitsRead);                        /*读取完第一个字节,下一个字节总是从第一个bit开始,所以从置 CurrBitNum */                        CurrBitNum = 0;                        byte byMask = 0;                        byMask = (byte)(Math.Pow((float)2.0, (int)nCurrBitsToRead) - 1);                        //更新信号值                        nSigValueInBits |= (int)(byMsgByteVal & byMask) << nBitsRead;                        nBitsRead += nCurrBitsToRead;                    }                }            }            return nSigValueInBits;        }        //计算需要读取的字节数        private static int nGetNoOfBytesToRead(int nBitNum, int nSigLen)        {            int nBytesToRead = 1; //第一个字节            int nRemainingLength = nSigLen - (8 - nBitNum);            if (nRemainingLength > 0)            {                // 计算总共包含的字节数                nBytesToRead += (int)(nRemainingLength / 8);                // 计算跨越到下一个字节的bit数.                int nTotalBitsConsidered = ((nBytesToRead - 1) * 8) + (8 - nBitNum);                if (nTotalBitsConsidered < (int)nSigLen)                {                    nBytesToRead++;                }            }            return nBytesToRead;        }        //判断信号长度是否超过了消息数组        private static bool bValidateSignal(int nDLC, int nByteNum, int nBitNum, int nLength, int bDataFormat)        {            bool bValid = true;            int nBytesToRead = nGetNoOfBytesToRead(nBitNum, nLength);            bValid = (bDataFormat == 1) ? (int)(nByteNum + nBytesToRead - 1) <= nDLC : (int)(nByteNum - nBytesToRead) >= 0;            return bValid;        }        public static int  bytesToInt(byte[] src)        {            int value;            value = (int)(((src[0] << 8) & 0xFF00)                    | (src[1] & 0xFF));            return value;        }
 
你可能感兴趣的文章
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
备忘:java中的递归
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
<转>文档视图指针互获
查看>>