Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
System.Base64FormattingOptions F# snippet
  • Loading branch information
albert-du committed Dec 25, 2021
commit 191a710cdc9bb8da7bf534fcfbe7547ddc460260
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <Snippet3>
open System

// Define a byte array.
let bytes =
[| for i = 0 to 99 do byte (i + 1) |]

let originalTotal = Array.sumBy int bytes

// Display summary information about the array.
printfn "The original byte array:"
printfn $" Total elements: {bytes.Length}"
printfn $" Length of String Representation: {BitConverter.ToString(bytes).Length}"
printfn $" Sum of elements: {originalTotal:N0}"
printfn ""

// Convert the array to a base 64 string.
let s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks)
printfn $"The base 64 string:\n {s}\n"

// Restore the byte array.
let newBytes = Convert.FromBase64String s

let newTotal = Array.sumBy int newBytes

// Display summary information about the restored array.
printfn $" Total elements: {newBytes.Length}"
printfn $" Length of String Representation: {BitConverter.ToString(newBytes).Length}"
printfn $" Sum of elements: {newTotal:N0}"

// The example displays the following output:
// The original byte array:
// Total elements: 100
// Length of String Representation: 299
// Sum of elements: 5,050
//
// The base 64 string:
// AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5
// Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA==
//
// Total elements: 100
// Length of String Representation: 299
// Sum of elements: 5,050
// </Snippet3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="ToBase64String3.fs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions xml/System/Base64FormattingOptions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
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:

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.Convert.ToBase64String/cs/ToBase64String3.cs" interactive="try-dotnet" id="Snippet3":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.Convert.ToBase64String/fs/ToBase64String3.fs" id="Snippet3":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.Convert.ToBase64String/vb/ToBase64String3.vb" id="Snippet3":::

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.
Expand Down