[Mono-dev] Are there any endian issues with unsafe code?

Kornél Pál kornelpal at hotmail.com
Tue Nov 8 09:55:14 EST 2005


Hi,

Could you please run the attached test case with Mono on a big-endian system
and share the results with me to get to know the exact details of managed
endiannes.

Thanks.

Kornél

----- Original Message -----
From: "Brion Vibber" <brion at pobox.com>
To: "Kornél Pál" <kornelpal at hotmail.com>; <mono-devel-list at lists.ximian.com>
Sent: Tuesday, November 08, 2005 3:29 AM
Subject: Re: [Mono-dev] Are there any endian issues with unsafe code?


> Kornél Pál wrote:
>> I only use Mono on little-endian systems so I have no experience in Mono
>> on
>> big-endian systems.
>>
>> When I read int32 from an array of bytes using unsafe code running on
>> Mono
>> will I get the same int32 value on big-endian systems as on
>> little-endian?
>
> That depends on whether your read code takes endianness into account. ;)
>
> If you do something like this, you'll get different results:
>
> public class UnsafeTest {
> public static void Main(string[] args) {
> byte[] bits = {0x12, 0x34, 0x56, 0x78}; // ABCD
> int data;
> unsafe {
> byte *ptr = (byte *)&data;
> for (int i = 0; i < 4; i++)
> *(ptr++) = bits[i];
> }
> System.Console.WriteLine("0x{0:X}", data);
> }
> }
>
> On a big-endian PowerPC this displays: 0x12345678
> On a little-endian x86, this displays: 0x78563412
>
> I'm not sure if there's a standard way of asking the runtime for the
> processor's endianness, but it's easy enough to check by poking at a
> known int value with a byte pointer.
>
> -- brion vibber (brion @ pobox.com)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ManagedEndianTest.tar.gz
Type: application/x-gzip
Size: 3330 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20051108/bfabf950/attachment.gz 


More information about the Mono-devel-list mailing list