Bytes manipulation

  • Thread starter Thread starter LEM
  • Start date Start date
L

LEM

Hi all,

Perhaps an easy question, but I haven't been able to find the solution.

Let's suppose that I have this:

byte a[5];
a[0]=0x01;
a[1]=0x6B;

I would like to concatenate all the values from that array (from left to
right) and get the decimal value.

For this example, it would be 363 (0x016B)
How can I get that 363?

Thanks!
 
LEM,

Pass the array to the static ToInt32 method on the BitConverter class.
It will take the four bytes and convert them to an int.

Hope this helps.
 
LEM said:
Hi all,

Perhaps an easy question, but I haven't been able to find the solution.

Let's suppose that I have this:

byte a[5];
a[0]=0x01;
a[1]=0x6B;

I would like to concatenate all the values from that array (from left to
right) and get the decimal value.

For this example, it would be 363 (0x016B)
How can I get that 363?

Thanks!

Someone might come up with a better way to do this...I would be glad to know
if there was one :)

private int GetIntFromBytes(byte[] Bytes)
{
int num = 0;
for (int i = 0; i < Bytes.Length; i++) {
num += Bytes << ((Bytes.Length - (i + 1)) * 8);
}
return num;
}

Depending on the location (from left-to-right) we do a left-bitshift to
"move" the value into it's respective place in the binary world. Therefore,
we move 0x01 to the 1 0000 0000 position and leave the right-most 8-bits in
place...which gives us 0000 0001 0110 1011 which equals 363. Wrote into a
method that will allow calculating using these same rules on any byte-array
that follows these rules :)

HTH,
Mythran
 
LEM said:
Hi all,

Perhaps an easy question, but I haven't been able to find the solution.

Let's suppose that I have this:

byte a[5];
a[0]=0x01;
a[1]=0x6B;

I would like to concatenate all the values from that array (from left to
right) and get the decimal value.

For this example, it would be 363 (0x016B)
How can I get that 363?

Use the BitConverter class. If you want to be able to specify the
endianness, you can use my EndianBitConverter class from my miscutil
library:
http://www.pobox.com/~skeet/csharp/miscutil
 
Nicholas Paldino said:
LEM,

Pass the array to the static ToInt32 method on the BitConverter class.
It will take the four bytes and convert them to an int.

Hope this helps.

Because the OP only has a 2-byte byte-array, the ToInt32 method throws an
exception. Without padding, it will fail.

Mythran
 
Actually the OP had a 5 byte array but only showed the first two bytes being
initialised.
 
Susan Mackay said:
Actually the OP had a 5 byte array but only showed the first two bytes
being
initialised.

Yup, yer right. Like I said, he has a 5-byte array :P

heh, I failed to see the big '5' inside the array declaration :) I guess it
won't throw an exception since byte is a value type (I believe) therefore
the empty value of a byte would be 0x0...correct?

Mythran
 
Mythran said:
Yup, yer right. Like I said, he has a 5-byte array :P

heh, I failed to see the big '5' inside the array declaration :) I guess it
won't throw an exception since byte is a value type (I believe) therefore
the empty value of a byte would be 0x0...correct?

Correct.

On the other hand, he won't get the result he wants. Only four bytes of
the array will be used, but converting {0x01, 0x6b, 0x00, 0x00} will
either result in 0x016b0000 or 0x6b01, depending on the endianness.
It's not entirely clear why the OP expects to get 0x016b when
considering "all the values from that array".
 
Thanks a lot for all your answers. I'll get back if
I still have problems!

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top