Skip to content

Commit cd08a43

Browse files
authored
System.BitConverter F# snippets (dotnet#7530)
* System.BitConverter F# snippets * Edit bitstodbl.fs
1 parent b004abc commit cd08a43

File tree

39 files changed

+1563
-0
lines changed

39 files changed

+1563
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module bitconv
2+
3+
//<Snippet1>
4+
open System
5+
6+
let print: obj -> obj -> unit = printfn "%25O%30O"
7+
8+
let aDoubl = 0.1111111111111111111
9+
let aSingl = 0.1111111111111111111f
10+
let aLong = 1111111111111111111L
11+
let anInt = 1111111111
12+
let aShort = 11111s
13+
let aChar = '*'
14+
let aBool = true
15+
16+
printfn "This example of methods of the BitConverter class\ngenerates the following output.\n"
17+
print "argument" "byte array"
18+
print "--------" "----------"
19+
20+
// Convert values to Byte arrays and display them.
21+
print aDoubl (BitConverter.ToString(BitConverter.GetBytes aDoubl))
22+
23+
print aSingl (BitConverter.ToString(BitConverter.GetBytes aSingl))
24+
25+
print aLong (BitConverter.ToString(BitConverter.GetBytes aLong))
26+
27+
print anInt (BitConverter.ToString(BitConverter.GetBytes anInt))
28+
29+
print aShort (BitConverter.ToString(BitConverter.GetBytes aShort))
30+
31+
print aChar (BitConverter.ToString(BitConverter.GetBytes aChar))
32+
33+
print aBool (BitConverter.ToString(BitConverter.GetBytes aBool))
34+
35+
36+
// This example of methods of the BitConverter class
37+
// generates the following output.
38+
//
39+
// argument byte array
40+
// -------- ----------
41+
// 0.111111111111111 1C-C7-71-1C-C7-71-BC-3F
42+
// 0.1111111 39-8E-E3-3D
43+
// 1111111111111111111 C7-71-C4-2B-AB-75-6B-0F
44+
// 1111111111 C7-35-3A-42
45+
// 11111 67-2B
46+
// * 2A-00
47+
// True 01
48+
//</Snippet1>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module example1
2+
3+
// <Snippet3>
4+
open System
5+
6+
let value = -16
7+
let bytes = BitConverter.GetBytes value
8+
9+
// Convert bytes back to int.
10+
let intValue = BitConverter.ToInt32(bytes, 0)
11+
printfn $"""{value} = {intValue}: {if value.Equals intValue then "Round-trips" else "Does not round-trip"}"""
12+
13+
// Convert bytes to UInt32.
14+
let uintValue = BitConverter.ToUInt32(bytes, 0)
15+
printfn $"""{value} = {uintValue}: {if value.Equals uintValue then "Round-trips" else "Does not round-trip"}"""
16+
17+
18+
// The example displays the following output:
19+
// -16 = -16: Round-trips
20+
// -16 = 4294967280: Does not round-trip
21+
// </Snippet3>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="example1.fs" />
8+
<Compile Include="networkorder1.fs" />
9+
<Compile Include="bitconv.fs" />
10+
<Compile Include="littleend.fs" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//<Snippet2>
2+
open System
3+
4+
printfn $"This example of the BitConverter.IsLittleEndian field generates \nthe following output when run on x86-class computers.\n"
5+
printfn $"IsLittleEndian: {BitConverter.IsLittleEndian}"
6+
7+
// This example of the BitConverter.IsLittleEndian field generates
8+
// the following output when run on x86-class computers.
9+
//
10+
// IsLittleEndian: True
11+
//</Snippet2>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module networkorder1
2+
3+
// <Snippet4>
4+
open System
5+
6+
let value = 12345678
7+
let bytes = BitConverter.GetBytes value
8+
printfn $"{BitConverter.ToString bytes}"
9+
10+
if BitConverter.IsLittleEndian then
11+
Array.Reverse bytes
12+
13+
printfn $"{BitConverter.ToString bytes}"
14+
// Call method to send byte stream across machine boundaries.
15+
16+
// Receive byte stream from beyond machine boundaries.
17+
printfn $"{BitConverter.ToString bytes}"
18+
if BitConverter.IsLittleEndian then
19+
Array.Reverse bytes
20+
21+
printfn $"{BitConverter.ToString bytes}"
22+
let result = BitConverter.ToInt32(bytes, 0)
23+
24+
printfn $"Original value: {value}"
25+
printfn $"Returned value: {result}"
26+
27+
// The example displays the following output on a little-endian system:
28+
// 4E-61-BC-00
29+
// 00-BC-61-4E
30+
// 00-BC-61-4E
31+
// 4E-61-BC-00
32+
// Original value: 12345678
33+
// Returned value: 12345678
34+
// </Snippet4>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module bitstodbl
2+
3+
//<Snippet1>
4+
open System
5+
6+
let print obj1 obj2 =
7+
printfn $"{obj1,20}{obj2,27:E16}"
8+
9+
// Reinterpret the long argument as a double.
10+
let longBitsToDouble argument =
11+
let doubleValue = BitConverter.Int64BitsToDouble argument
12+
13+
// Display the argument in hexadecimal.
14+
print (String.Format("0x{0:X16}", argument)) doubleValue
15+
16+
printfn "This example of the BitConverter.Int64BitsToDouble(Int64) \nmethod generates the following output.\n"
17+
print "long argument" "double value"
18+
print "-------------" "------------"
19+
20+
// Convert long values and display the results.
21+
longBitsToDouble 0
22+
longBitsToDouble 0x3FF0000000000000L
23+
longBitsToDouble 0x402E000000000000L
24+
longBitsToDouble 0x406FE00000000000L
25+
longBitsToDouble 0x41EFFFFFFFE00000L
26+
longBitsToDouble 0x3F70000000000000L
27+
longBitsToDouble 0x3DF0000000000000L
28+
longBitsToDouble 0x0000000000000001L
29+
longBitsToDouble 0x000000000000FFFFL
30+
longBitsToDouble 0x0000FFFFFFFFFFFFL
31+
longBitsToDouble 0xFFFFFFFFFFFFFFFFL
32+
longBitsToDouble 0xFFF0000000000000L
33+
longBitsToDouble 0x7FF0000000000000L
34+
longBitsToDouble 0xFFEFFFFFFFFFFFFFL
35+
longBitsToDouble 0x7FEFFFFFFFFFFFFFL
36+
longBitsToDouble Int64.MinValue
37+
longBitsToDouble Int64.MaxValue
38+
39+
40+
// This example of the BitConverter.Int64BitsToDouble( long )
41+
// method generates the following output.
42+
43+
// long argument double value
44+
// ------------- ------------
45+
// 0x0000000000000000 0.0000000000000000E+000
46+
// 0x3FF0000000000000 1.0000000000000000E+000
47+
// 0x402E000000000000 1.5000000000000000E+001
48+
// 0x406FE00000000000 2.5500000000000000E+002
49+
// 0x41EFFFFFFFE00000 4.2949672950000000E+009
50+
// 0x3F70000000000000 3.9062500000000000E-003
51+
// 0x3DF0000000000000 2.3283064365386963E-010
52+
// 0x0000000000000001 4.9406564584124654E-324
53+
// 0x000000000000FFFF 3.2378592100206092E-319
54+
// 0x0000FFFFFFFFFFFF 1.3906711615669959E-309
55+
// 0xFFFFFFFFFFFFFFFF NaN
56+
// 0xFFF0000000000000 -∞
57+
// 0x7FF0000000000000 ∞
58+
// 0xFFEFFFFFFFFFFFFF -1.7976931348623157E+308
59+
// 0x7FEFFFFFFFFFFFFF 1.7976931348623157E+308
60+
// 0x8000000000000000 0.0000000000000000E+000
61+
// 0x7FFFFFFFFFFFFFFF NaN
62+
//</Snippet1>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module dbltobits
2+
3+
//<Snippet2>
4+
open System
5+
6+
let print obj1 obj2 =
7+
printfn $"{obj1,25:E16}{obj2,23:X16}"
8+
9+
// Reinterpret the double argument as a long.
10+
let doubleToLongBits argument =
11+
let longValue = BitConverter.DoubleToInt64Bits argument
12+
13+
// Display the resulting long in hexadecimal.
14+
print argument longValue
15+
16+
printfn "This example of the BitConverter.DoubleToInt64Bits(Double) \nmethod generates the following output.\n"
17+
print "double argument" "hexadecimal value"
18+
print "---------------" "-----------------"
19+
20+
// Convert double values and display the results.
21+
doubleToLongBits 1.0
22+
doubleToLongBits 15.0
23+
doubleToLongBits 255.0
24+
doubleToLongBits 4294967295.0
25+
doubleToLongBits 0.00390625
26+
doubleToLongBits 0.00000000023283064365386962890625
27+
doubleToLongBits 1.234567890123E-300
28+
doubleToLongBits 1.23456789012345E-150
29+
doubleToLongBits 1.2345678901234565
30+
doubleToLongBits 1.2345678901234567
31+
doubleToLongBits 1.2345678901234569
32+
doubleToLongBits 1.23456789012345678E+150
33+
doubleToLongBits 1.234567890123456789E+300
34+
doubleToLongBits Double.MinValue
35+
doubleToLongBits Double.MaxValue
36+
doubleToLongBits Double.Epsilon
37+
doubleToLongBits Double.NaN
38+
doubleToLongBits Double.NegativeInfinity
39+
doubleToLongBits Double.PositiveInfinity
40+
41+
42+
// This example of the BitConverter.DoubleToInt64Bits( double )
43+
// method generates the following output.
44+
45+
// double argument hexadecimal value
46+
// --------------- -----------------
47+
// 1.0000000000000000E+000 3FF0000000000000
48+
// 1.5000000000000000E+001 402E000000000000
49+
// 2.5500000000000000E+002 406FE00000000000
50+
// 4.2949672950000000E+009 41EFFFFFFFE00000
51+
// 3.9062500000000000E-003 3F70000000000000
52+
// 2.3283064365386963E-010 3DF0000000000000
53+
// 1.2345678901230000E-300 01AA74FE1C1E7E45
54+
// 1.2345678901234500E-150 20D02A36586DB4BB
55+
// 1.2345678901234565E+000 3FF3C0CA428C59FA
56+
// 1.2345678901234567E+000 3FF3C0CA428C59FB
57+
// 1.2345678901234569E+000 3FF3C0CA428C59FC
58+
// 1.2345678901234569E+150 5F182344CD3CDF9F
59+
// 1.2345678901234569E+300 7E3D7EE8BCBBD352
60+
// -1.7976931348623157E+308 FFEFFFFFFFFFFFFF
61+
// 1.7976931348623157E+308 7FEFFFFFFFFFFFFF
62+
// 4.9406564584124654E-324 0000000000000001
63+
// NaN FFF8000000000000
64+
// -∞ FFF0000000000000
65+
// ∞ 7FF0000000000000
66+
//</Snippet2>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="dbltobits.fs" />
8+
<Compile Include="bitstodbl.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module bytesbool
2+
3+
//<Snippet1>
4+
open System
5+
6+
// Define Boolean true and false values.
7+
let values = [ true; false ]
8+
9+
// Display the value and its corresponding byte array.
10+
printfn "%10s%16s\n" "Boolean" "Bytes"
11+
12+
for value in values do
13+
let bytes = BitConverter.GetBytes value
14+
15+
BitConverter.ToString bytes
16+
|> printfn "%10b%16s" value
17+
18+
19+
// The example displays the following output:
20+
// Boolean Bytes
21+
//
22+
// true 01
23+
// false 00
24+
//</Snippet1>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module byteschar
2+
3+
//<Snippet2>
4+
open System
5+
6+
let print obj1 obj2 =
7+
printfn $"{obj1,10}{obj2,16}"
8+
9+
// Convert a char argument to a byte array and display it.
10+
let getBytesChar (argument: char) =
11+
let byteArray = BitConverter.GetBytes argument
12+
13+
BitConverter.ToString byteArray
14+
|> print argument
15+
16+
printfn "This example of the BitConverter.GetBytes(char) \nmethod generates the following output.\r\n"
17+
print "char" "byte array"
18+
print "----" "----------"
19+
20+
// Convert char values and display the results.
21+
getBytesChar '\000'
22+
getBytesChar ' '
23+
getBytesChar '*'
24+
getBytesChar '3'
25+
getBytesChar 'A'
26+
getBytesChar '['
27+
getBytesChar 'a'
28+
getBytesChar '{'
29+
30+
31+
// This example of the BitConverter.GetBytes(char)
32+
// method generates the following output.
33+
//
34+
// char byte array
35+
// ---- ----------
36+
// 00-00
37+
// 20-00
38+
// * 2A-00
39+
// 3 33-00
40+
// A 41-00
41+
// [ 5B-00
42+
// a 61-00
43+
// { 7B-00
44+
//</Snippet2>

0 commit comments

Comments
 (0)