Skip to content

Commit cfb5a74

Browse files
committed
Struct Snippet Update
1 parent 3232e28 commit cfb5a74

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

CSharp Code Samples/CodeSamples/Structures/StructSample.cs

+69-2
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,87 @@ public FourBytesMakeAnInteger(byte a, byte b, byte c, byte d)
3939
[FieldOffset(3)]
4040
public readonly byte D;
4141

42-
[FieldOffset(0)]
42+
[FieldOffset(0)]
4343
public readonly uint UnsignedInteger;
4444
}
4545

46+
47+
[StructLayout(LayoutKind.Explicit)]
48+
public struct FloatAndUint
49+
{
50+
[FieldOffset(0)]
51+
public float FloatValue;
52+
53+
[FieldOffset(0)]
54+
public uint UintValue;
55+
}
56+
57+
[StructLayout(LayoutKind.Explicit)]
58+
public struct DoubleAndUlong
59+
{
60+
[FieldOffset(0)]
61+
public double DoubleValue;
62+
63+
[FieldOffset(0)]
64+
public ulong UlongValue;
65+
}
66+
67+
[Flags]
68+
internal enum DosFileAttributes : byte
69+
{
70+
None = 0,
71+
ReadOnly = 1,
72+
Archive = 2,
73+
Compressed = 4,
74+
Hidden = 8,
75+
System = 16,
76+
Encrypted = 32,
77+
Indexed = 64,
78+
Temporary = 128
79+
}
80+
81+
[StructLayout(LayoutKind.Explicit)]
82+
internal struct EnumInStruct
83+
{
84+
[FieldOffset(0)]
85+
public uint FileHeader;
86+
87+
[FieldOffset(3)]
88+
public DosFileAttributes Attributes;
89+
}
90+
4691
public override void Execute()
4792
{
48-
Title("FilesSampleExecute");
93+
Title("StructSampleExecute");
4994

95+
Section("Compose bytes into int");
5096
var makeAnIntFromBytes = new FourBytesMakeAnInteger(0x11, 0x22, 0x33, 0x44);
5197
Console.WriteLine($"We have four bytes (0x{makeAnIntFromBytes.A:X2}, 0x{makeAnIntFromBytes.B:X2}, 0x{makeAnIntFromBytes.C:X2}, 0x{makeAnIntFromBytes.D:X2}), we composed them into an integer (0x{makeAnIntFromBytes.UnsignedInteger:X8}).");
5298

5399
var makeBytesFromInt = new FourBytesMakeAnInteger(0xFFEEDDAA);
54100
Console.WriteLine($"We have an integer (0x{makeBytesFromInt.UnsignedInteger:X8}) we divided into four bytes (0x{makeBytesFromInt.A:X2}, 0x{makeBytesFromInt.B:X2}, 0x{makeBytesFromInt.C:X2}, 0x{makeBytesFromInt.D:X2}).");
55101

102+
Section("Convert float to uint");
103+
var floatAndUint = new FloatAndUint();
104+
floatAndUint.FloatValue = 3.1415926f;
105+
Console.WriteLine($"We have an float ({floatAndUint.FloatValue}) we can show as uint (in hex) = (0x{floatAndUint.UintValue:X4}).");
106+
floatAndUint.UintValue = 0x40490fda; //PI in IEEE-754 format
107+
Console.WriteLine($"We have an uint (in hex) = (0x{floatAndUint.UintValue:X4}) we can show as float = ({floatAndUint.FloatValue}).");
108+
109+
Section("Convert double to ulong");
110+
var doubleAndUlong = new DoubleAndUlong();
111+
doubleAndUlong.DoubleValue = Math.E;
112+
Console.WriteLine($"We have an float ({doubleAndUlong.DoubleValue}) we can show as uint (in hex) = (0x{doubleAndUlong.UlongValue:X8}).");
113+
doubleAndUlong.UlongValue = 0x400921fb54442d18; //PI in IEEE-754 format
114+
Console.WriteLine($"We have an uint (in hex) = (0x{doubleAndUlong.UlongValue:X8}) we can show as float = ({doubleAndUlong.DoubleValue}).");
115+
116+
Section("Convert float to uint");
117+
var random = new Random();
118+
byte attributes = (byte)random.Next(1, 255);
119+
var enumInStruct = new EnumInStruct();
120+
enumInStruct.FileHeader = (uint)((attributes << 24) | 0x345678);
121+
Console.WriteLine($"We have an Fileheader (in hex) = (0x{enumInStruct.FileHeader:X4}), we can extract Attributes = ({enumInStruct.Attributes.ToString("g")}).");
122+
56123
Finish();
57124
}
58125
}

0 commit comments

Comments
 (0)