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

byte order and endianness

Iam trying to pack some bits and create a client request packet using a
Byte[] array. To test the "endianess" I tried a small sample and used the
BitConverter to print the actual values ... and the out does not seem right
..... the sample code and the output is shown below.

What Am I missing here ?

using System;
....
byte b = 10;
uint val = b;
Byte[] packet = BitConverter.GetBytes(b);

BitArray array = new
BitArray(packet);

IEnumerator enumerator = array.GetEnumerator();

string value = null;
int width = 8;

if(BitConverter.IsLittleEndian)
Console.WriteLine("Little Endian order.");
else
Console.WriteLine("Big Endian order.");

while(enumerator.MoveNext())
{
if(width == 0)
{
Console.Write(" ");
width = 8;
}

value = (enumerator.Current.ToString() == Boolean.TrueString)?"1":"0";
Console.Write(value);
width--;
}
Output
=========

Little Endian order.
01010000 00000000

Nov 16 '05 #1
1 4727
Senthil <Se*****@discussions.microsoft.com> wrote:
Iam trying to pack some bits and create a client request packet using a
Byte[] array. To test the "endianess" I tried a small sample and used the
BitConverter to print the actual values ... and the out does not seem right
.... the sample code and the output is shown below.

What Am I missing here ?


BitArray doesn't work the way you think it does. From the docs for the
constructor of BitArray you're using:

<quote>
The first byte in the array represents bits 0 through 7, the second
byte represents bits 8 through 15, and so on. The Least Significant Bit
of each byte represents the lowest index value: " bytes [0] & 1"
represents bit 0, " bytes [0] & 2" represents bit 1, " bytes [0] & 4"
represents bit 2, and so on.
</quote>

And that's eactly what you're seeing. Byte 0 is 10, and byte 1 is 0.
That means that bit 0 of your bit array is 0, bit 1 is 1 (which is
bytes[0] & 2), bit 2 is 0, bit 3 is 1 (which is bytes[0] & 8) etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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

Similar topics

4
by: Gernot Frisch | last post by:
Hi, when porint apps to powerpc the byte order get's swapped to low-endian. Now, what problems can occur and what should I take care of when porting to mac e.g.? -- -Gernot int main(int...
2
by: Donal McWeeney | last post by:
Hi, I think I may have a slight encoding problem with what I am doing. I have a test console app that takes an xml document and splits it into a number of smaller xml documents. First thing...
2
by: Jesse Engle | last post by:
i'm learning how to do some basic low-level network programming. the site i'm reading talks about "network byte order" and "host byte order". the thing is, it doesn't give an explanation as to what...
6
by: hmoulding | last post by:
This may be a FAQ, in which case you all may (probably will) yell at me. I haven't coded in plain C for almost 20 years, so I hope the following code is actually done right. Suppose the...
3
by: Scott | last post by:
I'm writing a C# server application that is reading from a socket sent from a C++ client program. The C++ client program is sending the following data: struct { int stringLen; actual ASCII...
12
by: Michi Henning | last post by:
Looking at the language spec, I can't find a statement about the byte order for value types, such as int, float, etc. Are they guaranteed to be little-endian or big- endian? I know that, on a...
4
by: joey.powell | last post by:
Hello, I need to be able to read a binary file that contains fields written in BOTH big endian and little endian byte orders. I am currently unable to read the big endian fields (erroneous...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
11
by: nephish | last post by:
hello there, all. i have a difficult app that connects to a server to get information for our database here. this server is our access point to some equipment in the field that we monitor. ...
3
by: velpur | last post by:
Dear friends, ( Suppose we change byte orders. For example, our program reads a file as a character string and converts the byte order. ) // a simple code #define INTEL_CPU #ifdef...
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...

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.