473,545 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7300
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*******@disc ussions.microso ft.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.Is LittleEndian.

-Rob Teixeira [MVP]

"Vijaye Raji" <no************ @hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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*******@disc ussions.microso ft.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.Is LittelEndian 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.Is LittelEndian 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.ByteBu ffer.

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.HostT oNetworkOrder

and

IPAddress.Netwo rkToHostOrder

Hope this helps.
--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain

Nov 15 '05 #6
Michi Henning <an*******@disc ussions.microso ft.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.co m>
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.HostT oNetworkOrder

and

IPAddress.Netwo rkToHostOrder


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*******@disc ussions.microso ft.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.Ge tBytes(char), BitConverter.Ge tBytes(int)
etc.

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

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

Similar topics

2
4528
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 they are. i understand most of what the main socket functions do, but i don't understand what's up with this network byte order stuff. could...
3
1878
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 { byte pB = new byte;
6
10154
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 the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array)...
6
2179
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. So, I can just assign an int to a char to retrieve the rightest byte of that int, and I also use this method plus right-shift operation to get...
33
3370
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-byte value. Can I use the arithmatic shift operators to hide the endian-ness of the underlying processor when assembling a native 4-byte value...
4
2206
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, and so on. The code computes at runtime the byte-order of the unsigned integer, but alas it would be better if it could be determined at...
11
1935
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. the messages come in over a socket connection. And according to their (very limited) documentation, are set up in a particular order. like this
5
3454
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 the int as:
3
4499
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 to serialize the whole newly-defined container, in order to copy the contents to another array. So i thought to define an iterator which...
0
7935
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...
0
7780
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...
0
6009
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...
1
5351
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...
0
3479
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...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.