将字节数组转换为整数 [英] Converting a byte array to integer

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

问题描述

大家好,



我需要将公共静态字节[]转换为整数。

我的代码如下。 />


Hi all,

I need to convert the public static byte [] to integer.
My code as follows.

public static byte[] ORIGINAL_DATA_BYTE=null;
...
...
ADVERTISEMENT_DATA_BYTE[10] ---> This will have a hex value "a6" which is equivalent to 166;
...
Byte numdata = ADVERTISEMENT_DATA_BYTE[10];
String result = new String();
result = String.format(%02X, numdata);
int noofdata = Integer.parseInt(result, 16);
if(noofdata == 166)
{
    but my code not comes in this block
}
....
....









提前致谢。





Thanks in advance.

推荐答案

获取一个字节值,然后将其转换为字符串以将其解析为整数是疯了。



你应该意识到字节值基本上是一个数字,因为它是一个整数值,并且不需要转换任何东西,更不用说在中间使用字符串...



a6不等于166,a6和166是表示相同价值的两种方式。



所以你应该写一下

Taking a byte value, then transforming it to a string to parse it to an integer is insane.

You should realize that a byte value is basically a number, as is an integer value, and there is no need to convert anything, let alone using a string in the middle...

a6 is not equivalent to 166, a6 and 166 are two ways to represent THE SAME VALUE.

So you just should write
int noofdata = (int)numdata;
// OR
int noofdata = (int)ADVERTISEMENT_DATA_BYTE[10];





但我认为你应该彻底研究什么是数据类型(在.NET中,在开发中)以及如何使用它们。



But I think you should study toroughly what are datatypes (in .NET, and in development in general) and how to use them.


在C#中的


: -



BitConverter.ToInt32(arrayByte,0));
in C#:-

BitConverter.ToInt32(arrayByte, 0));


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

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