473,396 Members | 1,775 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,396 software developers and data experts.

BitConverter.ToInt16() Trying to understand "behind the scenes"

This probably something stupid, or I am missing some fundemantal concept, but
I can't figure this one out.

Consider the following code:

byte[] bd = new byte[2];
bd[0] = 0x00;
bd[1] = 0x01;

System.Int16 a;
a = System.BitConverter.ToInt16(bd, 0);
MessageBox.Show(a.ToString());

BitConverter.ToInt16 returns 16 bit signed integer at specified position in
a byte array.
So, one would think hex value 0x0001 is 1 in decimal, no the following code
produces 256 which is 0x0010. Now switch around values in byte array:

byte[] bd = new byte[2];
bd[0] = 0x01;
bd[1] = 0x00;

System.Int16 a;
a = System.BitConverter.ToInt16(bd, 0);
MessageBox.Show(a.ToString());

The code above does produce 1. But hex 0x0100 is 256.

Can someone explain this?
Nov 17 '05 #1
7 8087
Hi,

It called little endian, a byte ( 16 bits ) are stored reversed, it's been
a architecture of Intel processors since the beginning, I don;t remember
right now the reasons though.

a search for little endian got me this link in google:
http://www.cs.umass.edu/~verts/cs32/endian.html
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Lenn" <Le**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
This probably something stupid, or I am missing some fundemantal concept,
but
I can't figure this one out.

Consider the following code:

byte[] bd = new byte[2];
bd[0] = 0x00;
bd[1] = 0x01;

System.Int16 a;
a = System.BitConverter.ToInt16(bd, 0);
MessageBox.Show(a.ToString());

BitConverter.ToInt16 returns 16 bit signed integer at specified position
in
a byte array.
So, one would think hex value 0x0001 is 1 in decimal, no the following
code
produces 256 which is 0x0010. Now switch around values in byte array:

byte[] bd = new byte[2];
bd[0] = 0x01;
bd[1] = 0x00;

System.Int16 a;
a = System.BitConverter.ToInt16(bd, 0);
MessageBox.Show(a.ToString());

The code above does produce 1. But hex 0x0100 is 256.

Can someone explain this?

Nov 17 '05 #2
"Lenn" <Le**@discussions.microsoft.com> wrote:
This probably something stupid, or I am missing some fundemantal concept, but
I can't figure this one out.

Consider the following code:

byte[] bd = new byte[2];
bd[0] = 0x00;
bd[1] = 0x01;

System.Int16 a;
a = System.BitConverter.ToInt16(bd, 0);
MessageBox.Show(a.ToString());

BitConverter.ToInt16 returns 16 bit signed integer at specified position in
a byte array.
So, one would think hex value 0x0001 is 1 in decimal, no the following code
produces 256 which is 0x0010. Now switch around values in byte array:

byte[] bd = new byte[2];
bd[0] = 0x01;
bd[1] = 0x00;

System.Int16 a;
a = System.BitConverter.ToInt16(bd, 0);
MessageBox.Show(a.ToString());

The code above does produce 1. But hex 0x0100 is 256.

Can someone explain this?


The program runs on a little endian machine.
See static function BitConverter.IsLittleEndian

Sergei
Nov 17 '05 #3
"Lenn" <Le**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
This probably something stupid, or I am missing some fundemantal concept,
but
I can't figure this one out. <snip> byte[] bd = new byte[2];
bd[0] = 0x01;
bd[1] = 0x00;

System.Int16 a;
a = System.BitConverter.ToInt16(bd, 0);
MessageBox.Show(a.ToString());

The code above does produce 1. But hex 0x0100 is 256.

Can someone explain this?


Yes.

the data is stored in little-endian order //low order bytes before high
order bytes
This is the Norm for Windows based systems.

Other Some operating systems use Big-endian ordering.
I think Linux varies by the machine it runs on. (Not sure on this)

This is one of the reasons that it is difficult to transfer binary data
between different systems.
You need to agree which endianness is the "Correct" one.

Hope this helps
Bill



Nov 17 '05 #4


Lenn wrote:
This probably something stupid, or I am missing some fundemantal concept, but
I can't figure this one out.


There are two (actually more, but common anyway :) ways to write
composed numbers: Most or Least Significant Byte first (MSB/LSB,
Big/Little-endian respectively). MSB is the usual notation used by
humans. LSB is used by many CPU's, including x86 variants.

(int)0x01020304

in MSB:

[0x01,0x02,0x03,0x04]

in LSB:

[0x04,0x03,0x02,0x01]

You can check in BitConverter.IsLittleEndian what endianness
BitConverter will use.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #5
Thank you all.

I had no knoweledge about Little endian.
They probably had good reasons to do that. I wonder what they are. I hope it
wasn't to confuse me :)

That brings another question. What are the specific cases when I need to
worry about it.
For example this code:

int x = 0;
x = 0x0001;
MessageBox.Show(x.ToString());

works as expected, so my hex 0x0001 represntation of 1 internally is really
stored as 0100. But if I wanted to create a byte array with 2 elements
representing 16-bit integer I would put 0x01 before 0x00, right? Can you tell
me any other gotchas?

"Helge Jensen" wrote:


Lenn wrote:
This probably something stupid, or I am missing some fundemantal concept, but
I can't figure this one out.


There are two (actually more, but common anyway :) ways to write
composed numbers: Most or Least Significant Byte first (MSB/LSB,
Big/Little-endian respectively). MSB is the usual notation used by
humans. LSB is used by many CPU's, including x86 variants.

(int)0x01020304

in MSB:

[0x01,0x02,0x03,0x04]

in LSB:

[0x04,0x03,0x02,0x01]

You can check in BitConverter.IsLittleEndian what endianness
BitConverter will use.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Nov 17 '05 #6
Lenn <Le**@discussions.microsoft.com> wrote:
I had no knoweledge about Little endian.
They probably had good reasons to do that. I wonder what they are. I hope it
wasn't to confuse me :)

That brings another question. What are the specific cases when I need to
worry about it.
For example this code:

int x = 0;
x = 0x0001;
MessageBox.Show(x.ToString());

works as expected, so my hex 0x0001 represntation of 1 internally is really
stored as 0100. But if I wanted to create a byte array with 2 elements
representing 16-bit integer I would put 0x01 before 0x00, right? Can you tell
me any other gotchas?


It's really only when you want to convert to or from a byte array - a
bit like converting characters using a specific encoding.

While you're within the confines of actual numbers, the endianness
doesn't matter at all, as you've seen - 0x0001 is 1 whatever the
endianness is.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Or**************@tk2msftngp13.phx.gbl...
It called little endian, a byte ( 16 bits ) are stored reversed, it's been a architecture of Intel processors since the beginning, I don;t remember
right now the reasons though.


They aren't "reversed". And Intel's reason for doing it this way is
because it's the most natural & correct way of doing it. Little Endian
order puts the lowest order byte at the lowest order address. It merely
appears "reversed" due to the way English-speaking humans read hardcopy
print-out of hex dumps.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #8

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

Similar topics

5
by: george r smith | last post by:
Hi, I created a byte array --byte aByteArray = new byte; and set aByteArray and aByteArray to '1'. Because I want to create a ulong (UInt64) with these values I do: UInt64 bb; bb =...
10
by: sparks | last post by:
So far I have tried the microsoft version of this, and lost the table LOl so I tried this way to write to two tables carrying over the autoid but so far I can not get the autonumber and put it in...
1
by: Michael Davidov | last post by:
byte array = {0x00,0x04}; short ashort = BitConverter.ToInt16(array,0); Console.WriteLine(ashort.ToString()); Why am I wrong to think that this should return a 4 instead of 1024? Also, is...
12
by: Dica | last post by:
i can't seem to find a way to cast the value in my comboBox to integer. i've tried the following: short iProjectID = Convert.ToInt16(cboProjects.SelectedValue); i keep getting a "Sepcified...
3
by: skosmicki | last post by:
I need to create an function similar to the "MATCH" function in Excel that evaluates a number within a set of numbers and returns whether there is a match. I have put the example of what I see in...
5
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
Hello. I have a problem with getting short value from 2 byte array. I have this code. There are 2 short values in bytes. byte cast = { 18, 152, 00, 80 }; Int32 port = BitConverter.ToInt16(cast,...
1
by: DR | last post by:
mySqlBytes.buffer is getting converted to BigEndian even though both SQL server 2005 and the CLR function are on the same machine which shows BitConverter.IsLittleEndian == true in tsql: select...
6
by: Bishop | last post by:
a += (UInt32)(url + (url << 8) + (url << 16) + (url << 24)); I'm trying to understand what the above line of code does. I understand that the url part of (url << 8) returns part of the string...
5
by: DaveD | last post by:
Can anyone help me get this compiled ? void Write<T>(T val) { byte bytes = BitConverter.GetBytes(val); Array.Reverse(bytes); writer.Write(bytes); } The problem is that for T=bool,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.