vb.net 在VB.Net中将字节数组转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/227083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Byte Array to Integer In VB.Net
提问by Kibbee
I'm wonder what the best way to convert a byte array (length 4) to an integer is in 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. Along the same lines, what about converting a single/double from it's binary representation to an single/double variable.
我想知道在 vb.net 中将字节数组(长度为 4)转换为整数的最佳方法是什么?我知道 BitConverter,但是执行函数调用来执行应该能够通过复制 4 个字节的内存来完成的事情似乎很浪费。同样,如何将单/双从其二进制表示转换为单/双变量。
回答by mdb
"Copying bytes of memory" is something that .NET isn't particularly suited for (and VB.NET even less so). So, unless switching to C is an option for you, a function call is pretty much unavoidable for this.
“复制内存字节”是 .NET 不是特别适合的事情(VB.NET 更不适合)。因此,除非切换到 C 是您的一个选择,否则函数调用几乎是不可避免的。
BitConverter is a well-thought-out, tested function. Of course, you can avoid it by doing something like (in C#):
BitConverter 是一个经过深思熟虑、经过测试的功能。当然,您可以通过执行以下操作(在 C# 中)来避免它:
myInt = (*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24);
(which is, incidentally, exactlywhat 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;
- Doesn't do any of the error checking that BitConverter does for you;
- Doesn't differentiate between little-endian and big-endian representations, like BitConverter does.
- 比 BitConverter 等价物更难阅读和理解;
- 不做任何 BitConverter 为你做的错误检查;
- 不像 BitConverter 那样区分小端和大端表示。
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 的设计非常非常好,您在使用其功能时不应三思而后行,除非您遇到实际(性能)问题。
回答by Jon Skeet
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 convincedthat you have a performance bottleneck in this exact piece of code, use the functionality provided by the framework.
除非您完全确信这段代码存在性能瓶颈,否则请使用框架提供的功能。
回答by sbeskur
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))
回答by Ihar Bury
You can block copy byte[] to int[] using System.Buffer class.
您可以使用 System.Buffer 类阻止将 byte[] 复制到 int[]。