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

No defined byte order for C#?

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 little-endian machine, the byte
order is little-endian. But what if I run C# on a big-
endian machine, say, using Mono?

Does the C# language (or IL) make any guarantees as to the
byte order?

Thanks,

Michi.
Nov 15 '05 #1
12 7292
It's the runtime that needs to take care of the endian issues. Not the
language by itself.

There are some implementations of the .Net runtime on Big Endian machines
and the same code compiles fine and runs on both machines (with special
attention to endian differences, of course)

-vJ

"Michi Henning" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
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 little-endian machine, the byte
order is little-endian. But what if I run C# on a big-
endian machine, say, using Mono?

Does the C# language (or IL) make any guarantees as to the
byte order?

Thanks,

Michi.

Nov 15 '05 #2
If you need to know, call BitConverter.IsLittleEndian.

-Rob Teixeira [MVP]

"Vijaye Raji" <no************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
It's the runtime that needs to take care of the endian issues. Not the
language by itself.

There are some implementations of the .Net runtime on Big Endian machines
and the same code compiles fine and runs on both machines (with special
attention to endian differences, of course)

-vJ

"Michi Henning" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
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 little-endian machine, the byte
order is little-endian. But what if I run C# on a big-
endian machine, say, using Mono?

Does the C# language (or IL) make any guarantees as to the
byte order?

Thanks,

Michi.


Nov 15 '05 #3
Michi Henning wrote:
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 little-endian machine, the byte
order is little-endian. But what if I run C# on a big-
endian machine, say, using Mono?

Does the C# language (or IL) make any guarantees as to the
byte order?


No. But if you really need to know (which should be relatively rare)
you can use the Bitconverter.IsLittelEndian field to determine the
underlying architecture.

I assume that the other BitConverter methods would take the endianness
into account (it doesn't seem to be explicitly documented in the SDK),
to make converting blobs of bytes (in byte arrays) to .NET native types
easy.

On the down-side, as far as I can tell BitConverter is not part of the
ECMA CLI standard, so it might not exist on a Mono (or other non-MS)
runtime for all I know.

--
mikeb
Nov 15 '05 #4
>> Does the C# language (or IL) make any guarantees as to
the
byte order?

No. But if you really need to know (which should be

relatively rare)you can use the Bitconverter.IsLittelEndian field to determine theunderlying architecture.
Ah, thanks. I'm unmarshaling a little-endian byte stream
and need to turn the data in the byte stream back into
real things, such as floats and ints. So, when I'm running
on a big-endian machine, I have to change the byte order
accordingly.

BitConverter is close to what I need. But, what do I do
when I'm running on a big-endian machine and need to
convert a little-endian byte stream? From what I can see,
there is no way to tell BitConverter what byte order the
array is in that is to be converted.

And, of course, I need to be able to do the opposite:
convert C# value types into a little-endian byte stream
(whether I'm running on a little-endian or big-endian
machine). Is there any class that does the inverse of what
BitConverter does?
I assume that the other BitConverter methods would take the endiannessinto account (it doesn't seem to be explicitly documented in the SDK),to make converting blobs of bytes (in byte arrays) to .NET native typeseasy.
Well, as far as I can see, there is no way to specify the
byte order of the byte array.
On the down-side, as far as I can tell BitConverter is not part of theECMA CLI standard, so it might not exist on a Mono (or other non-MS)runtime for all I know.


Aha. Thanks for pointing that out. I guess I can always
write it myself (but I don't see a way to do this without
having to resort to pointers and unsafe code.

What I'm really looking for is something along the lines
of java.nio.ByteBuffer.

Cheers,

Michi.

Nov 15 '05 #5
Hello:
And, of course, I need to be able to do the opposite:
convert C# value types into a little-endian byte stream
(whether I'm running on a little-endian or big-endian
machine).


Review :

IPAddress.HostToNetworkOrder

and

IPAddress.NetworkToHostOrder

Hope this helps.
--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain

Nov 15 '05 #6
Michi Henning <an*******@discussions.microsoft.com> wrote:
BitConverter is close to what I need. But, what do I do
when I'm running on a big-endian machine and need to
convert a little-endian byte stream? From what I can see,
there is no way to tell BitConverter what byte order the
array is in that is to be converted.


No. It's a pain, isn't it?

I've considered before writing a version of BitConverter that lets you
specify the byte order, or fetch the default. Would you be interested
in such a class if I wrote it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Carlos Guzmán Álvarez wrote:
Hello:
And, of course, I need to be able to do the opposite: convert C# value
types into a little-endian byte stream (whether I'm running on a
little-endian or big-endian machine).

Review :

IPAddress.HostToNetworkOrder

and

IPAddress.NetworkToHostOrder


Unfortunately for the original poster, Network Order is big-endian.

I think he'll need to write his own class based on BitConverter and/or
the IPAddress methods (or take Jon Skeet up on his offer).

--
mikeb
Nov 15 '05 #8
>> there is no way to tell BitConverter what byte order
the
array is in that is to be converted.
No. It's a pain, isn't it?

I've considered before writing a version of BitConverter

that lets youspecify the byte order, or fetch the default. Would you be interestedin such a class if I wrote it?


Absolutely! Bit converter really ought to be able to
convert between representations. And it should be
bidirectional, so I can convert from values to byte arrays
as well. (The introduction in the man page actually claims
that BitConverter can do this but, of course, the relevant
methods don't exist.)

Cheers,

Michi.

Nov 15 '05 #9
Michi Henning <an*******@discussions.microsoft.com> wrote:
Absolutely! Bit converter really ought to be able to
convert between representations. And it should be
bidirectional, so I can convert from values to byte arrays
as well. (The introduction in the man page actually claims
that BitConverter can do this but, of course, the relevant
methods don't exist.)


Yes they do - BitConverter.GetBytes(char), BitConverter.GetBytes(int)
etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
>> (The introduction in the man page actually claims
that BitConverter can do this but, of course, the relevant methods don't exist.)
Yes they do - BitConverter.GetBytes(char),

BitConverter.GetBytes(int)etc.


Ah, yes, thanks! Me blind... :-(

So, the only thing missing is being able to specify a byte
order for the array, so I can convert little-endian to big-
endian and vice versa.

Anyway, I wrote a class that is essentially identical to
java.nio.ByteBuffer, which does what I need.

Thanks for all the help!

Cheers,

Michi.

Nov 15 '05 #11
BTW, along the same lines - sending unicode strings over the network is going to be a pain too. Won't I need to convert every single character in the string using HostToNetworkOrder and then send out the resultiing buffer. Something along the lines of (its ugly and has no bounds checks)

int index = 0;
byte[] myBuffer = new byte[1024];
foreach (char c in myString)
{
byte[] hb = BitConverter.GetBytes(c);
short hs = BitConverter.ToInt16(hb, 0);
short ns = IPAddress.HostToNetworkOrder(hs);

byte[] nb = BitConverter.GetBytes(ns);
nb.CopyTo(myBuffer, index*2);
index++;
}

myNetworkStream.Write(myBuffer, 0, index*2);
Nov 15 '05 #12
srikant <an*******@discussions.microsoft.com> wrote:
BTW, along the same lines - sending unicode strings over the network
is going to be a pain too. Won't I need to convert every single
character in the string using HostToNetworkOrder and then send out the
resultiing buffer. Something along the lines of (its ugly and has no
bounds checks)

int index = 0;
byte[] myBuffer = new byte[1024];
foreach (char c in myString)
{
byte[] hb = BitConverter.GetBytes(c);
short hs = BitConverter.ToInt16(hb, 0);
short ns = IPAddress.HostToNetworkOrder(hs);

byte[] nb = BitConverter.GetBytes(ns);
nb.CopyTo(myBuffer, index*2);
index++;
}

myNetworkStream.Write(myBuffer, 0, index*2);


This is exactly what Encodings are for - and getting the right encoding
(eg using Encoding.BigEndianUnicode or creating an instance of
UnicodeEncoding with the appropriate constructor) will do it all for
you.

(You also wouldn't need to use BitConverter in the above anyway - just
nb[index++]=(byte)(c>>8);
nb[index++]=(byte)(c&0xff);

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

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

Similar topics

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...
3
by: Ted Miller | last post by:
Hi folks, I've got an unmanaged routine I'm pinvoking to. It takes a pointer to an array of 3 pointers to bytes, typed as byte**. public static extern void foo(byte **p3pb); unsafe {...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
6
by: lovecreatesbeauty | last post by:
/* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char....
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
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. ...
5
by: moni | last post by:
Hey, My buffer contains a short int, some char, and a structure in form of a byte array. Read the string as: TextBox4.Text = System.Text.Encoding.ASCII.GetString(buffer1, 0, 31); Read...
3
by: vasili | last post by:
hello All, I have a simple issue. I defined a custom container, that encloses a std::list, which in turn holds objects that are a simple abstraction of a six position array. Now, i would like...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.