Click here to Skip to main content
15,844,903 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I have a byte value array containing elements, I want to get the value by block and convert to decimal, element block value in predefined. Assume, I have this code as below.

var _bytes= new byte[]{0, 12, 0, 6, 19, 136};
var _size = 3; //byte with 3 item is decimal
var _num = 2 ; //length of block define in array byte
var _result = "";
for (int i = 0; i < _size; i++)
{
   //get value decimal in byte array follow the block define 
   //var _id = BitConverter.ToInt16(_bytes[i] * 2, 2);
   _result += _id + " ";
}
//the _result should be "12 6 5000"
//0 12 is decimal 12
//0 6 is decimal 6
//19 136 is decimal 5000


What I have tried:

try to convert but result not correct
Posted
Updated 11-Jul-23 0:36am
v2
Comments
Richard MacCutchan 11-Jul-23 6:04am    
How do you get 5000 from 19 and 136?
headshot9x 11-Jul-23 21:54pm    
Convert 19, 136 to Hexa first , after that convert to Decimal , It's 5000

1 solution

Probably this is working

var _id = Convert.ToDecimal(Convert.ToInt32(BitConverter.ToInt16((_bytes[i] * 2, 2).Reverse().ToArray(), 0).ToString("X4"), 16));
 
Share this answer
 

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