简体   繁体   English

在VB.Net中将字节数组转换为整数

[英]Convert Byte Array to Integer In VB.Net

I'm wonder what the best way to convert a byte array (length 4) to an integer is in vb.net? 我想知道将字节数组(长度为4)转换为整数的最佳方法是在vb.net中? I'm aware of BitConverter, but it seems like quite a waste to do a function call to do something that should be able to be done by copying 4 bytes of memory. 我知道BitConverter,但是做一个函数调用做一些应该可以通过复制4个字节的内存完成的事情似乎是一种浪费。 Along the same lines, what about converting a single/double from it's binary representation to an single/double variable. 同样,如何将单个/双精度从它的二进制表示转换为单个/双变量。

"Copying bytes of memory" is something that .NET isn't particularly suited for (and VB.NET even less so). “复制内存字节”是.NET不是特别适合的(而VB.NET甚至更少)。 So, unless switching to C is an option for you, a function call is pretty much unavoidable for this. 因此,除非切换到C是一个选项,否则函数调用几乎是不可避免的。

BitConverter is a well-thought-out, tested function. BitConverter是经过深思熟虑,经过测试的功能。 Of course, you can avoid it by doing something like (in C#): 当然,您可以通过执行类似(在C#中)的操作来避免它:

myInt = (*pbyte) | (*(pbyte + 1) << 8)  | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24);

(which is, incidentally, exactly what BitConverter does for you when converting a byte array to an Integer...). (顺便说一下,在将字节数组转换为整数时,BitConverter 正是为您做的...)。

However, this code: 但是,这段代码:

  • Is much, much harder to read and understand than the BitConverter equivalent; 比BitConverter等效的更难阅读和理解;
  • Doesn't do any of the error checking that BitConverter does for you; 不执行BitConverter为您执行的任何错误检查;
  • Doesn't differentiate between little-endian and big-endian representations, like BitConverter does. 不像BitConverter那样区分little-endian和big-endian表示。

In other words: you might "save" a function call, but you will be significantly worse off in the end (even assuming you don't introduce any bugs). 换句话说:你可能会“保存”一个函数调用,但最终你会变得更糟(甚至假设你没有引入任何错误)。 In general, the .NET Framework is very, very well designed, and you shouldn't think twice about using its functionality, unless you encounter actual (performance) issues with it. 一般来说,.NET Framework设计得非常非常好,除非遇到实际(性能)问题,否则不应该再考虑使用它的功能。

I'm aware of BitConverter, but it seems like quite a waste to do a function call to do something that should be able to be done by copying 4 bytes of memory. 我知道BitConverter,但是做一个函数调用做一些应该可以通过复制4个字节的内存完成的事情似乎是一种浪费。

Whereas I view the situation as "it seems quite a waste trying to hand-code an efficient way of doing this when there's already a method call which does exactly what I want." 然而,我认为情况是“当一个方法调用完全符合我的要求时,尝试手动编写一种有效的方法来进行编码似乎是一种浪费。”

Unless you're absolutely convinced that you have a performance bottleneck in this exact piece of code, use the functionality provided by the framework. 除非您完全确信在这段代码中存在性能瓶颈,否则请使用框架提供的功能。

mdb is exactly correct, but, here is some code to convert a vb byte array to little endian integer anyway...(just in case you wish to write your own bitconverter class) mdb是完全正确的,但是,这里有一些代码将vb字节数组转换为小端整数...(以防你想编写自己的bitconverter类)

' where bits() is your byte array of length 4 '其中bits()是长度为4的字节数组

Dim i as Integer 

i = (((bits(0) Or (bits(1) << 8)) Or (bits(2) << &H10)) Or (bits(3) << &H18))

您可以使用System.Buffer类将byte []复制到int []。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM