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. 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.
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.
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
>> 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.
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
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
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
>> 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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;
|
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)...
|
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...
|
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...
| |
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...
|
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
|
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:
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |