[Mono-winforms-list] Endians

Kornél Pál kornelpal at gmail.com
Mon Jul 17 07:22:38 EDT 2006


Hi,

BitConverter uses the same byte order as the machine (that is stored in 
BitConverter.IsLittleEndian as well).

As far as I know only BinaryReader and BinaryWriter are little-endian.

If you need a specific byte order you have to use shift operators.

For an example and for more details please have a look at the source code of 
our BitConverter, BinaryReader and BinaryWriter classes.

Your GetBytesLittleEndian seems to be little-endian. But I think using shift 
operations is easier:
byte[] bytearray = new byte [4];
bytearray [0] = (byte) number;
bytearray [1] = (byte) (number >> 8);
bytearray [2] = (byte) (number >> 16);
bytearray [3] = (byte) (number >> 24);

Kornél

----- Original Message ----- 
From: "Pavel Bansky" <levap at bansky.net>
To: "winforms" <mono-winforms-list at lists.ximian.com>
Sent: Monday, July 17, 2006 9:06 AM
Subject: Re: [Mono-winforms-list] Endians


Thanks for your fast reply Kornel.
I'am interested especialy in BitConverter.GetBytes()
I wrote me following little function which should return always
little-endian array. Is this right or BitConverter always return
little-endian array?

byte[] GetBytesLittleEndian(int number)
{
byte[] bytearray = BitConverter.GetBytes(number);

if ( ! BitConverter.IsLittleEndian ) Array.Reverse(bytearray);

return(bytearray);
}


have a nice day

  Pavel


Kornél Pál wrote:

> Hi,
>
> Have a look at this message and the messages referred to in it:
> http://lists.ximian.com/pipermail/mono-devel-list/2005-December/016352.html
>
> Kornél
>
> ----- Original Message ----- From: "Pavel Bansky" <levap at bansky.net>
> To: "winforms" <mono-winforms-list at lists.ximian.com>
> Sent: Sunday, July 16, 2006 6:48 PM
> Subject: [Mono-winforms-list] Endians
>
>
> Hi all,
>
> I have a little question (maybe a bit off-topic). Does MS.NET and Mono
> use the same endians on every machines? I mean when I write integer into
> binary file, will be the byte order same on all architectures - or the
> framework respects endians for current architecture?
>
>  Thanx a lot
>
>  Pavel
>

-- 
__________________________________________________________

Pavel Bánský
levap at bansky.net                        I write code...
__________________________________________________________
_______________________________________________
Mono-winforms-list maillist  -  Mono-winforms-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-winforms-list 



More information about the Mono-winforms-list mailing list