转换字节数组整数VB.Net [英] Convert Byte Array to Integer In VB.Net

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

问题描述

我不知道为一个字节数组(长度4)转换为整数的最好方式是在vb.net?我知道BitConverter,但它似乎是相当浪费做一个函数调用做一些事情,应该能够通过复制4字节的内存来完成。按照同样的思路,那从它转换单/双的二元再presentation到单/双变量。

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.

推荐答案

复制字节的内存的东西,.NET不是特别适合(和VB.NET就更少了)。所以,除非切换到C是一个选择,一个函数调用pretty很多不可避免的这一点。

"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.

BitConverter是一个深思熟虑的,测试的功能。当然,你可以通过执行类似(在C#)避免:

BitConverter is a well-thought-out, tested function. Of course, you can avoid it by doing something like (in C#):

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

(这是顺便提一句,究竟转换为字节数组时,整数...什么BitConverter为你做)。

(which is, incidentally, exactly what BitConverter does for you when converting a byte array to an Integer...).

不过,这code:


  • 多,更难阅读和理解比BitConverter同等学历;

  • 不做任何错误检查BitConverter为您做的;

  • 不小端和大端再presentations区分,像BitConverter一样。

在换句话说:你可以挽救一个函数调用,但你会在最后显著更糟(即使假设你没有引入任何错误)。一般情况下,.NET框架是非常,非常好设计的,你不应该三思而后使用它的功能,除非你遇到的实际(性能)的问题吧。

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.

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

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