Skip to content

Commit 5d1c631

Browse files
authored
System.Base64FormattingOptions F# snippet (#7528)
1 parent fcb2620 commit 5d1c631

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// <Snippet3>
2+
open System
3+
4+
// Define a byte array.
5+
let bytes =
6+
[| for i = 0 to 99 do byte (i + 1) |]
7+
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}"
15+
printfn ""
16+
17+
// Convert the array to a base 64 string.
18+
let s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks)
19+
printfn $"The base 64 string:\n {s}\n"
20+
21+
// Restore the byte array.
22+
let newBytes = Convert.FromBase64String s
23+
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="ToBase64String3.fs" />
8+
</ItemGroup>
9+
</Project>

xml/System/Base64FormattingOptions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
The following example calls the <xref:System.Convert.ToBase64String%28System.Byte%5B%5D%2CSystem.Base64FormattingOptions%29?displayProperty=nameWithType> method with a `InsertLineBreaks` argument to insert line breaks in the string that is produced by encoding a 100-element byte array:
5959
6060
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.Convert.ToBase64String/cs/ToBase64String3.cs" interactive="try-dotnet" id="Snippet3":::
61+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.Convert.ToBase64String/fs/ToBase64String3.fs" id="Snippet3":::
6162
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.Convert.ToBase64String/vb/ToBase64String3.vb" id="Snippet3":::
6263
6364
As the output from the example shows, the <xref:System.Convert.FromBase64String%2A?displayProperty=nameWithType> succeeds in restoring the original byte array; the line break characters are ignored during the conversion.

0 commit comments

Comments
 (0)