473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Bytes manipulation

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!
Sep 20 '06 #1
8 1958
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" <an*******@nospam.comwrote in message
news:uE**************@TK2MSFTNGP02.phx.gbl...
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!

Sep 20 '06 #2

"LEM" <an*******@nospam.comwrote in message
news:uE**************@TK2MSFTNGP02.phx.gbl...
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[i] << ((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
Sep 20 '06 #3
LEM <an*******@nospam.comwrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 20 '06 #4

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:e$**************@TK2MSFTNGP04.phx.gbl...
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
Sep 20 '06 #5
Actually the OP had a 5 byte array but only showed the first two bytes being
initialised.
On 21/9/06 06:06, in article Ov**************@TK2MSFTNGP02.phx.gbl,
"Mythran" <ki********@hotmail.comwrote:
>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:e$**************@TK2MSFTNGP04.phx.gbl...
>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

Sep 20 '06 #6

"Susan Mackay" <ma******@hotmail.comwrote in message
news:C1********************@hotmail.com...
Actually the OP had a 5 byte array but only showed the first two bytes
being
initialised.
On 21/9/06 06:06, in article Ov**************@TK2MSFTNGP02.phx.gbl,
"Mythran" <ki********@hotmail.comwrote:
>>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in
message news:e$**************@TK2MSFTNGP04.phx.gbl...
>>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

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
Sep 20 '06 #7
Mythran <ki********@hotmail.comwrote:
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".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #8
LEM
Thanks a lot for all your answers. I'll get back if
I still have problems!

Regards
Sep 21 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tony C | last post by:
Does Python have a function that is analogous to C's write() or fwrite()- that is , I want to write a file (of arbitrary data) that is 100K, or 1MB (or more) bytes long.. Both write() and...
12
by: jmoy | last post by:
I have some data (say in a file) that needs to be handled byte by byte. Source code I have looked at does this by treating the data as a stream of 'char's. However, the standard does not require a...
9
by: I. Kobrinsky | last post by:
I'm new here. I started a personal password-program, a trial that includes username, logincounter and password. So my intention is to hide pwd while tipping. So I'm thinking about two popular...
25
by: Allan Rydberg | last post by:
hi i'm trying to shift a double, but i'm getting the error message '>>' illegal, left operand double. althought that the manpages say, '>>' and '<<' can be applied for int's only, i was able...
9
by: Job | last post by:
Hi, I would like to find out what ASP/ASP.net can do with image manipulation. Does ASP have built in functions (eg. after upload to server) to manipulate images, like rotate, scale, crop etc.?...
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
2
by: jackmejia | last post by:
Hello all. I am mostly a C# developer, and I am working on a research project which involves networking between a server written in C# and the client written in C++. I have defined a protocol...
6
by: Bint | last post by:
I have an array whose elements I'm accessing, like array, array, etc. However, the data is meant to be 16-bit words, not bytes. I'm getting byte values right now. Is there any way I can tell php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.