It is really convenient to use it to pick up the following:
C # using the FieldOffset property for common and forced type conversions
These two days have been somewhat unaccustomed to the coercion type conversion of C #. That's the way it goes.
In C #, I'm going to read the binary text. The structure of the file is simple, and a series of tightly stored int32 values are stored in binary mode. Now I want to randomly read the I values starting with nth int32 and read them into the array. The result check C # can only read to byte[], or read out a loop int[]. The pursuit of efficiency of course I do not want this, if you can like C + + as the byte[] cast to int[] just fine. For example:
[CPP]View PlainCopyprint?
- Char tmp[64]; //16 int. Equivalent to C # byte[] tmp = new int[64];
- int* dat; //DAT pointer. Equivalent to C # int[] dat;
- Ifstream IF (...); //standard file input stream. Equivalent to C # FileStream FS = new FileStream (...);
- If.read (TMP, 64); //Read 64 bytes. Equivalent to C # FS. Read (tmp,0,64);
- DAT = (int*) tmp; //To convert the first address of the TMP array to an int type, the array is converted to an array of type int with 4 bytes.
- //The data does not change in memory because the data is originally of type int.
- //The key is that this step cannot be directly implemented in C #.
- for (int i = 0; i <; i++)
- cout << Dat[i] << ""; //Output data. Equivalent to C # console.write (dat[i]+ "");
In C # can not be directly implemented, for this I thought a lot of ways, also looked for a long time, until I saw this code:
[CSharp]View PlainCopyprint?
- Using System;
- Using System.IO;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Provides a wide variety of members that support COM interop and platform invoke services
- Using System.Runtime.InteropServices;
- Namespace test
- {
- Class Program
- {
- //StructLayout enables designers to control the physical layout of data fields for a class or structure
- //explicit with FieldOffset to control the precise location of each data member
- [StructLayout (LAYOUTKIND.EXPLICIT)]
- public struct S1
- {
- //FieldOffset control field has a physical position offset of 0
- [FieldOffset (0)]
- public byte[] A;
- //The same offset is 0, and the starting position overlaps with a.
- [FieldOffset (0)]
- public int[] b;
- }
- static void Main (string[] args)
- {
- S1 s = new S1 (); //To new, otherwise B will report "Use a field that may not be assigned value"
- s.a=new byte[64];
- FileStream FS = new FileStream ("E:\\test.txt", FileMode.Open);
- //S.A. to receive the data of the file
- Fs. Read (s.a,0,64);
- //Make sure you don't cross the border when you read B
- For (int i = 0; i <; i++)
- Console.Write (s.b[i]+""); //b to use the data
- Console.readkey ();
- }
- }
- }
The principle is, in fact, like C + +, to point a int[] type variable to the same memory area as the byte[] type variable, which is the same as the first address in C + + where dat points to a char array. Similarly, such structural functions are the same as for C + +, where a structure can be used as a variety of data types, depending on what type is the case.
The files used in the test will not be uploaded, there is a winhex can edit their own, there is no way to write the program will be from 0xF to 0x0 16 numbers in binary mode output to the file. The contents of the file are opened in Winhex with the 16 binary display should be as follows:
How C # uses the shared body