Skip to content

Commit 63b021e

Browse files
authored
System.Convert F# snippets (#7688)
* Convert F# snippets * Add missing snippets * fix snippet ref
1 parent 343dd5f commit 63b021e

File tree

103 files changed

+9233
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+9233
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module ToBase64String
2+
3+
// <Snippet2>
4+
open System
5+
6+
let displayArray (arr: 'a[]) =
7+
printfn "The array:"
8+
printf "{ "
9+
for i = 0 to arr.GetUpperBound(0) - 1 do
10+
printf $"{arr[i]}, "
11+
if (i + 1) % 10 = 0 then
12+
printf "\n "
13+
printfn $"{arr[arr.GetUpperBound 0]} }}\n"
14+
15+
// Define an array of 20 elements and display it.
16+
let arr = Array.zeroCreate<int> 20
17+
let mutable value = 1
18+
for i = 0 to arr.GetUpperBound 0 do
19+
arr[i] <- value
20+
value <- value * 2 + 1
21+
displayArray arr
22+
23+
// Convert the array of integers to a byte array.
24+
let bytes = Array.zeroCreate<byte> (arr.Length * 4)
25+
for i = 0 to arr.Length - 1 do
26+
Array.Copy(BitConverter.GetBytes(arr[i]), 0, bytes, i * 4, 4)
27+
28+
// Encode the byte array using Base64 encoding
29+
let base64 = Convert.ToBase64String bytes
30+
printfn "The encoded string: "
31+
printfn $"{base64}\n"
32+
33+
// Convert the string back to a byte array.
34+
let newBytes = Convert.FromBase64String base64
35+
36+
// Convert the byte array back to an integer array.
37+
let newArr = Array.zeroCreate<int> (newBytes.Length / 4)
38+
for i = 0 to newBytes.Length / 4 - 1 do
39+
newArr[i] <- BitConverter.ToInt32(newBytes, i * 4)
40+
41+
displayArray newArr
42+
43+
// The example displays the following output:
44+
// The array:
45+
// { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
46+
// 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
47+
//
48+
// The encoded string:
49+
// AQAAAAMAAAAHAAAADwAAAB8AAAA/AAAAfwAAAP8AAAD/AQAA/wMAAP8HAAD/DwAA/x8AAP8/AAD/fwAA//8AAP//AQD//wMA//8HAP//DwA=
50+
//
51+
// The array:
52+
// { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
53+
// 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
54+
// </Snippet2>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module ToBase64String2
2+
3+
// <Snippet1>
4+
open System
5+
6+
// Define a byte array.
7+
let bytes = [| 2uy; 4uy; 6uy; 8uy; 10uy; 12uy; 14uy; 16uy; 18uy; 20uy |]
8+
printfn $"The byte array:\n {BitConverter.ToString bytes}\n"
9+
10+
// Convert the array to a base 64 string.
11+
let s = Convert.ToBase64String bytes
12+
printfn $"The base 64 string:\n {s}\n"
13+
14+
// Restore the byte array.
15+
let newBytes = Convert.FromBase64String s
16+
printfn $"The restored byte array:\n {BitConverter.ToString newBytes}\n"
17+
18+
// The example displays the following output:
19+
// The byte array:
20+
// 02-04-06-08-0A-0C-0E-10-12-14
21+
//
22+
// The base 64 string:
23+
// AgQGCAoMDhASFA==
24+
//
25+
// The restored byte array:
26+
// 02-04-06-08-0A-0C-0E-10-12-14
27+
// </Snippet1>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module ToBase64String3
2+
3+
// <Snippet3>
4+
open System
5+
6+
// Define a byte array.
7+
let bytes = Array.init 100 (fun i -> i + 1 |> byte)
8+
let originalTotal = Array.sumBy int bytes
9+
10+
// Display summary information about the array.
11+
printfn "The original byte array:"
12+
printfn $" Total elements: {bytes.Length}"
13+
printfn $" Length of String Representation: {BitConverter.ToString(bytes).Length}"
14+
printfn $" Sum of elements: {originalTotal:N0}\n"
15+
16+
// Convert the array to a base 64 string.
17+
let s =
18+
Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks)
19+
20+
printfn $"The base 64 string:\n {s}\n"
21+
22+
// Restore the byte array.
23+
let newBytes = Convert.FromBase64String s
24+
let newTotal = Array.sumBy int newBytes
25+
26+
// Display summary information about the restored array.
27+
printfn $" Total elements: {newBytes.Length}"
28+
printfn $" Length of String Representation: {BitConverter.ToString(newBytes).Length}"
29+
printfn $" Sum of elements: {newTotal:N0}"
30+
31+
// The example displays the following output:
32+
// The original byte array:
33+
// Total elements: 100
34+
// Length of String Representation: 299
35+
// Sum of elements: 5,050
36+
//
37+
// The base 64 string:
38+
// AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5
39+
// Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA==
40+
//
41+
// Total elements: 100
42+
// Length of String Representation: 299
43+
// Sum of elements: 5,050
44+
// </Snippet3>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="ToBase64String.fs" />
8+
<Compile Include="ToBase64String2.fs" />
9+
<Compile Include="ToBase64String3.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module ToByte5
2+
3+
// <Snippet15>
4+
open System
5+
6+
let values =
7+
[| null; ""; "0xC9"; "C9"; "101"; "16.3"; "$12"
8+
"$12.01"; "-4"; "1,032"; "255"; " 16 " |]
9+
10+
for value in values do
11+
try
12+
let number = Convert.ToByte(value)
13+
printfn $"""'%A{value}' --> {number}"""
14+
with
15+
| :? FormatException ->
16+
printfn $"Bad Format: '%A{value}'"
17+
| :? OverflowException ->
18+
printfn $"OverflowException: '{value}'"
19+
20+
// The example displays the following output:
21+
// '<null>' --> 0
22+
// Bad Format: ''
23+
// Bad Format: '0xC9'
24+
// Bad Format: 'C9'
25+
// '101' --> 101
26+
// Bad Format: '16.3'
27+
// Bad Format: '$12'
28+
// Bad Format: '$12.01'
29+
// OverflowException: '-4'
30+
// Bad Format: '1,032'
31+
// '255' --> 255
32+
// ' 16 ' --> 16
33+
// </Snippet15>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="tobyte1.fs" />
8+
<Compile Include="tobyte2.fs" />
9+
<Compile Include="tobyte3.fs" />
10+
<Compile Include="tobyte4.fs" />
11+
<Compile Include="ToByte5.fs" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
module tobyte1
2+
3+
open System
4+
5+
let convertBoolean () =
6+
// <Snippet1>
7+
let falseFlag = false
8+
let trueFlag = true
9+
10+
printfn $"{falseFlag} converts to {Convert.ToByte falseFlag}."
11+
printfn $"{trueFlag} converts to {Convert.ToByte trueFlag}."
12+
// The example displays the following output:
13+
// False converts to 0.
14+
// True converts to 1.
15+
// </Snippet1>
16+
17+
let convertChar () =
18+
// <Snippet2>
19+
let chars = [| 'a'; 'z'; '\u0007'; '\u03FF' |]
20+
for ch in chars do
21+
try
22+
let result = Convert.ToByte ch
23+
printfn $"{ch} is converted to {result}."
24+
with :? OverflowException ->
25+
printfn $"Unable to convert u+{Convert.ToInt16 ch:X4} to a byte."
26+
// The example displays the following output:
27+
// a is converted to 97.
28+
// z is converted to 122.
29+
// is converted to 7.
30+
// Unable to convert u+03FF to a byte.
31+
// </Snippet2>
32+
33+
let convertInt16 () =
34+
// <Snippet3>
35+
let numbers = [| Int16.MinValue; -1s; 0s; 121s; 340s; Int16.MaxValue |]
36+
for number in numbers do
37+
try
38+
let result = Convert.ToByte number
39+
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
40+
with :? OverflowException ->
41+
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
42+
// The example displays the following output:
43+
// The Int16 value -32768 is outside the range of the Byte type.
44+
// The Int16 value -1 is outside the range of the Byte type.
45+
// Converted the Int16 value 0 to the Byte value 0.
46+
// Converted the Int16 value 121 to the Byte value 121.
47+
// The Int16 value 340 is outside the range of the Byte type.
48+
// The Int16 value 32767 is outside the range of the Byte type.
49+
// </Snippet3>
50+
51+
let convertInt32 () =
52+
// <Snippet4>
53+
let numbers = [| Int32.MinValue; -1; 0; 121; 340; Int32.MaxValue |]
54+
for number in numbers do
55+
try
56+
let result = Convert.ToByte number
57+
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
58+
with :? OverflowException ->
59+
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
60+
// The example displays the following output:
61+
// The Int32 value -2147483648 is outside the range of the Byte type.
62+
// The Int32 value -1 is outside the range of the Byte type.
63+
// Converted the Int32 value 0 to the Byte value 0.
64+
// Converted the Int32 value 121 to the Byte value 121.
65+
// The Int32 value 340 is outside the range of the Byte type.
66+
// The Int32 value 2147483647 is outside the range of the Byte type.
67+
// </Snippet4>
68+
69+
let convertInt64 () =
70+
// <Snippet5>
71+
let numbers = [| Int64.MinValue; -1L; 0L; 121L; 34L; Int64.MaxValue |]
72+
for number in numbers do
73+
try
74+
let result = Convert.ToByte number
75+
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
76+
with :? OverflowException ->
77+
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
78+
// The example displays the following output:
79+
// The Int64 value -9223372036854775808 is outside the range of the Byte type.
80+
// The Int64 value -1 is outside the range of the Byte type.
81+
// Converted the Int64 value 0 to the Byte value 0.
82+
// Converted the Int64 value 121 to the Byte value 121.
83+
// The Int64 value 340 is outside the range of the Byte type.
84+
// The Int64 value 9223372036854775807 is outside the range of the Byte type.
85+
// </Snippet5>
86+
87+
let convertObject () =
88+
// <Snippet6>
89+
let values: obj[] =
90+
[| true; -12; 163; 935; 'x'; "104"; "103.0"
91+
"-1"; "1.00e2"; "One"; 1.00e2 |]
92+
for value in values do
93+
try
94+
let result = Convert.ToByte value
95+
printfn $"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."
96+
with
97+
| :? OverflowException ->
98+
printfn $"The {value.GetType().Name} value {value} is outside the range of the Byte type."
99+
| :? FormatException ->
100+
printfn $"The {value.GetType().Name} value {value} is not in a recognizable format."
101+
| :? InvalidCastException ->
102+
printfn $"No conversion to a Byte exists for the {value.GetType().Name} value {value}."
103+
// The example displays the following output:
104+
// Converted the Boolean value True to the Byte value 1.
105+
// The Int32 value -12 is outside the range of the Byte type.
106+
// Converted the Int32 value 163 to the Byte value 163.
107+
// The Int32 value 935 is outside the range of the Byte type.
108+
// Converted the Char value x to the Byte value 120.
109+
// Converted the String value 104 to the Byte value 104.
110+
// The String value 103.0 is not in a recognizable format.
111+
// The String value -1 is outside the range of the Byte type.
112+
// The String value 1.00e2 is not in a recognizable format.
113+
// The String value One is not in a recognizable format.
114+
// Converted the Double value 100 to the Byte value 100.
115+
// </Snippet6>
116+
117+
let convertSByte () =
118+
// <Snippet7>
119+
let numbers = [| SByte.MinValue; -1y; 0y; 10y; SByte.MaxValue |]
120+
for number in numbers do
121+
try
122+
let result = Convert.ToByte number
123+
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
124+
with :? OverflowException ->
125+
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
126+
// The example displays the following output:
127+
// The SByte value -128 is outside the range of the Byte type.
128+
// The SByte value -1 is outside the range of the Byte type.
129+
// Converted the SByte value 0 to the Byte value 0.
130+
// Converted the SByte value 10 to the Byte value 10.
131+
// Converted the SByte value 127 to the Byte value 127.
132+
// </Snippet7>
133+
134+
let convertUInt16 () =
135+
// <Snippet8>
136+
let numbers = [| UInt16.MinValue; 121us; 340us; UInt16.MaxValue |]
137+
for number in numbers do
138+
try
139+
let result = Convert.ToByte number
140+
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
141+
with :? OverflowException ->
142+
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
143+
// The example displays the following output:
144+
// Converted the UInt16 value 0 to the Byte value 0.
145+
// Converted the UInt16 value 121 to the Byte value 121.
146+
// The UInt16 value 340 is outside the range of the Byte type.
147+
// The UInt16 value 65535 is outside the range of the Byte type.
148+
// </Snippet8>
149+
150+
let convertUInt32 () =
151+
// <Snippet9>
152+
let numbers = [| UInt32.MinValue; 121u; 340u; UInt32.MaxValue |]
153+
for number in numbers do
154+
try
155+
let result = Convert.ToByte number
156+
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
157+
with :? OverflowException ->
158+
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
159+
// The example displays the following output:
160+
// Converted the UInt32 value 0 to the Byte value 0.
161+
// Converted the UInt32 value 121 to the Byte value 121.
162+
// The UInt32 value 340 is outside the range of the Byte type.
163+
// The UInt32 value 4294967295 is outside the range of the Byte type.
164+
// </Snippet9>
165+
166+
let convertUInt64 () =
167+
// <Snippet10>
168+
let numbers= [| UInt64.MinValue; 121uL; 340uL; UInt64.MaxValue |]
169+
for number in numbers do
170+
try
171+
let result = Convert.ToByte number
172+
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
173+
with :? OverflowException ->
174+
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
175+
// The example displays the following output:
176+
// Converted the UInt64 value 0 to the Byte value 0.
177+
// Converted the UInt64 value 121 to the Byte value 121.
178+
// The UInt64 value 340 is outside the range of the Byte type.
179+
// The UInt64 value 18446744073709551615 is outside the range of the Byte type.
180+
// </Snippet10>
181+
182+
convertBoolean ()
183+
printfn "-----"
184+
convertChar ()
185+
printfn "-----"
186+
convertInt16 ()
187+
printfn "-----"
188+
convertInt32 ()
189+
printfn "-----"
190+
convertInt64 ()
191+
printfn "-----"
192+
convertObject ()
193+
printfn "-----"
194+
convertSByte ()
195+
printfn "-----"
196+
convertUInt16 ()
197+
printfn "-----"
198+
convertUInt32 ()
199+
printfn "-----"
200+
convertUInt64 ()

0 commit comments

Comments
 (0)