BitConverter.ToInt32转换2个字节 [英] BitConverter.ToInt32 to convert 2 bytes

查看:868
本文介绍了BitConverter.ToInt32转换2个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BitConverter.ToInt32将Byte数组转换为int。

I am using BitConverter.ToInt32 to convert a Byte array into int.

我只有两个字节[0] [26],但是该功能需要4个字节,所以我必须在现有字节的前面添加两个0字节。

I have only two bytes [0][26], but the function needs 4 bytes, so I have to add two 0 bytes to the front of the existing bytes.

最快的方法是什么。

谢谢。

推荐答案

您可能应该做(int)BitConverter.ToInt16(.. ) ToInt16 可以将两个字节读取为 short 。然后,您只需使用强制转换将其转换为 int

You should probably do (int)BitConverter.ToInt16(..) instead. ToInt16 is made to read two bytes into a short. Then you simply convert that to an int with the cast.

这篇关于BitConverter.ToInt32转换2个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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