Click here to Skip to main content
15,112,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
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.

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];

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

BitConverter.ToInt32(arrayByte, 0));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900