你如何转换3个字节为C#中的24位数字? [英] How do you convert 3 bytes into a 24 bit number in C#?

查看:638
本文介绍了你如何转换3个字节为C#中的24位数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我从一个消息的标题部分读取的字节数组。这些字节包含消息的长度。从来就没有超过3个字节,他们是从LSB到MSB订购。因此,在下面的例子中,39是LSB和2是MSB。

  VAR数据=新的字节[] {39, 213,2}; 

在上面的例子中我怎样才能把这些字节转换为数字(INT,短,等)?


解决方案

  VAR NUM =数据[0] +(数据[1] ;&所述; 8)+(数据[2]&下;&下; 16); 


I have an array of bytes that I read from a header section of a message. These bytes contain the length of the message. There never are more than 3 bytes and they are ordered from LSB to MSB. So in the example below, 39 is the LSB and 2 is the MSB.

var data = new byte[] { 39, 213, 2 };

In the example above how can I take those bytes and convert to a number (int,short,etc)?

解决方案

var num = data[0] + (data[1] << 8) + (data[2] << 16);

这篇关于你如何转换3个字节为C#中的24位数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆