473,656 Members | 2,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BitConverter.To Int16() 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.BitConve rter.ToInt16(bd , 0);
MessageBox.Show (a.ToString());

BitConverter.To Int16 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.BitConve rter.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 8117
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**@discussio ns.microsoft.co m> wrote in message
news:61******** *************** ***********@mic rosoft.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.BitConve rter.ToInt16(bd , 0);
MessageBox.Show (a.ToString());

BitConverter.To Int16 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.BitConve rter.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**@discussio ns.microsoft.co m> 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.BitConve rter.ToInt16(bd , 0);
MessageBox.Show (a.ToString());

BitConverter.To Int16 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.BitConve rter.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.Is LittleEndian

Sergei
Nov 17 '05 #3
"Lenn" <Le**@discussio ns.microsoft.co m> wrote in message
news:61******** *************** ***********@mic rosoft.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.BitConve rter.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.Is LittleEndian 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.Is LittleEndian 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**@discussio ns.microsoft.co m> 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.co m>
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.mach in AT dot.state.fl.us > wrote
in message news:Or******** ******@tk2msftn gp13.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
5367
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 = BitConverter.ToUInt64(aByteArray,0); Console.WriteLine(bb);
10
1443
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 the other table. thanks big time for any help maybe I tried to do it the dumb way but it looks to me like this should work probably not :)
1
2216
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 there a way to get a short value of 4 from those 2 bytes without switching their possition in the byte array?
12
4705
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 cast is not allowed" error. how can i do this?
3
1622
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 excel in the check column. The "0" answer in the result column is in the fourth account in the list. Somehow I need to loop through the accounts comparing the result to the total and indicate a match in the check column. It wouldn't even need...
5
4575
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, 0); // -26606 - wrong! Int32 port2 = BitConverter.ToInt16(cast, 2); // 20480 - wrong! //port and port2 get not correct values //and I found solution in google short result1 = (short)(((short)cast) | (short)(cast * 256)); //4760 -
1
1990
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 * from dbo.MY_CLR_FUNCTION(cast(1024 as binary(4))) public static int MY_CLR_FUNCTION(SqlBytes mySqlBytes) in debugger binaryData.buffer now shows bigendian!! 0 byte
6
1477
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 but I don't understand what the << 8 does.
5
5957
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, BitConverter.GetBytes(bool) won't compile. In C++ I would have created an explicit instantiation like
0
8382
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.