More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Sunday, March 17, 2019 3:06:01 PM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 11/23/2008(UTC)
Posts: 501

C# BitConverter and Endianness

C# BitConverter Converts base data types to an array of bytes, and an array of bytes to base data types, via static methods to convert each of the primitive types to and from an array of bytes,

To byte conversion

BitConverter.GetBytes()
Please note that the order of bytes in the array returned by the GetBytes method overloads depends on whether the computer architecture is little-endian or big-endian.

Input
double aDoubl = 0.1111111111111111111;
float aSingl = 0.1111111111111111111F;
long aLong = 1111111111111111111;
int anInt = 1111111111;
short aShort = 11111;
char aChar = '*';
bool aBool = true;

Output
argument byte array
-------- ----------
0.111111111111111 1C-C7-71-1C-C7-71-BC-3F
0.1111111 39-8E-E3-3D
1111111111111111111 C7-71-C4-2B-AB-75-6B-0F
1111111111 C7-35-3A-42
11111 67-2B
* 2A-00
True 01

From byte conversion
For example, BitConverter.ToInt32(bytes, 0);
Please note that the order of bytes in the array must reflect the endianness of the computer system's architecture.

The order of bytes in the array returned by the GetBytes method overloads depends on whether the computer architecture is little-endian or big-endian. Similarly, the order of bytes in the array and returned by the ToIntegerValue methods and the ToChar method depends on whether the computer architecture is little-endian or big-endian.

Little-endian means little end first while big end first.

The endianness of an architecture is indicated by the IsLittleEndian property.

Because the return value of some methods depends on system architecture, be careful when transmitting byte data beyond machine boundaries:

If all systems sending and receiving data are guaranteed to have the same endianness, nothing has be done to the data.

If systems sending and receiving data can have different endianness, always transmit data in a particular order. This means that the order of bytes in the array may have to be reversed either before sending them or after receiving them. A common convention is to transmit data in network byte order (big-endian order). The following example provides an implementation for sending an integer value in network byte order.
Code:

if (BitConverter.IsLittleEndian)
         Array.Reverse(bytes);

Edited by user Sunday, March 17, 2019 3:06:35 PM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.