Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Convert F# snippets #7688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module ToBase64String

// <Snippet2>
open System

let displayArray (arr: 'a[]) =
printfn "The array:"
printf "{ "
for i = 0 to arr.GetUpperBound(0) - 1 do
printf $"{arr[i]}, "
if (i + 1) % 10 = 0 then
printf "\n "
printfn $"{arr[arr.GetUpperBound 0]} }}\n"

// Define an array of 20 elements and display it.
let arr = Array.zeroCreate<int> 20
let mutable value = 1
for i = 0 to arr.GetUpperBound 0 do
arr[i] <- value
value <- value * 2 + 1
displayArray arr

// Convert the array of integers to a byte array.
let bytes = Array.zeroCreate<byte> (arr.Length * 4)
for i = 0 to arr.Length - 1 do
Array.Copy(BitConverter.GetBytes(arr[i]), 0, bytes, i * 4, 4)

// Encode the byte array using Base64 encoding
let base64 = Convert.ToBase64String bytes
printfn "The encoded string: "
printfn $"{base64}\n"

// Convert the string back to a byte array.
let newBytes = Convert.FromBase64String base64

// Convert the byte array back to an integer array.
let newArr = Array.zeroCreate<int> (newBytes.Length / 4)
for i = 0 to newBytes.Length / 4 - 1 do
newArr[i] <- BitConverter.ToInt32(newBytes, i * 4)

displayArray newArr

// The example displays the following output:
// The array:
// { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
// 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
//
// The encoded string:
// AQAAAAMAAAAHAAAADwAAAB8AAAA/AAAAfwAAAP8AAAD/AQAA/wMAAP8HAAD/DwAA/x8AAP8/AAD/fwAA//8AAP//AQD//wMA//8HAP//DwA=
//
// The array:
// { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
// 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module ToBase64String2

// <Snippet1>
open System

// Define a byte array.
let bytes = [| 2uy; 4uy; 6uy; 8uy; 10uy; 12uy; 14uy; 16uy; 18uy; 20uy |]
printfn $"The byte array:\n {BitConverter.ToString bytes}\n"

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

// Restore the byte array.
let newBytes = Convert.FromBase64String s
printfn $"The restored byte array:\n {BitConverter.ToString newBytes}\n"

// The example displays the following output:
// The byte array:
// 02-04-06-08-0A-0C-0E-10-12-14
//
// The base 64 string:
// AgQGCAoMDhASFA==
//
// The restored byte array:
// 02-04-06-08-0A-0C-0E-10-12-14
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module ToBase64String3

// <Snippet3>
open System

// Define a byte array.
let bytes = Array.init 100 (fun i -> i + 1 |> byte)
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}\n"

// 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,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="ToBase64String.fs" />
<Compile Include="ToBase64String2.fs" />
<Compile Include="ToBase64String3.fs" />
</ItemGroup>
</Project>
33 changes: 33 additions & 0 deletions snippets/fsharp/System/Byte/Overview/ToByte5.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module ToByte5

// <Snippet15>
open System

let values =
[| null; ""; "0xC9"; "C9"; "101"; "16.3"; "$12"
"$12.01"; "-4"; "1,032"; "255"; " 16 " |]

for value in values do
try
let number = Convert.ToByte(value)
printfn $"""'%A{value}' --> {number}"""
with
| :? FormatException ->
printfn $"Bad Format: '%A{value}'"
| :? OverflowException ->
printfn $"OverflowException: '{value}'"

// The example displays the following output:
// '<null>' --> 0
// Bad Format: ''
// Bad Format: '0xC9'
// Bad Format: 'C9'
// '101' --> 101
// Bad Format: '16.3'
// Bad Format: '$12'
// Bad Format: '$12.01'
// OverflowException: '-4'
// Bad Format: '1,032'
// '255' --> 255
// ' 16 ' --> 16
// </Snippet15>
13 changes: 13 additions & 0 deletions snippets/fsharp/System/Byte/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="tobyte1.fs" />
<Compile Include="tobyte2.fs" />
<Compile Include="tobyte3.fs" />
<Compile Include="tobyte4.fs" />
<Compile Include="ToByte5.fs" />
</ItemGroup>
</Project>
200 changes: 200 additions & 0 deletions snippets/fsharp/System/Byte/Overview/tobyte1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
module tobyte1

open System

let convertBoolean () =
// <Snippet1>
let falseFlag = false
let trueFlag = true

printfn $"{falseFlag} converts to {Convert.ToByte falseFlag}."
printfn $"{trueFlag} converts to {Convert.ToByte trueFlag}."
// The example displays the following output:
// False converts to 0.
// True converts to 1.
// </Snippet1>

let convertChar () =
// <Snippet2>
let chars = [| 'a'; 'z'; '\u0007'; '\u03FF' |]
for ch in chars do
try
let result = Convert.ToByte ch
printfn $"{ch} is converted to {result}."
with :? OverflowException ->
printfn $"Unable to convert u+{Convert.ToInt16 ch:X4} to a byte."
// The example displays the following output:
// a is converted to 97.
// z is converted to 122.
// is converted to 7.
// Unable to convert u+03FF to a byte.
// </Snippet2>

let convertInt16 () =
// <Snippet3>
let numbers = [| Int16.MinValue; -1s; 0s; 121s; 340s; Int16.MaxValue |]
for number in numbers do
try
let result = Convert.ToByte number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
// The example displays the following output:
// The Int16 value -32768 is outside the range of the Byte type.
// The Int16 value -1 is outside the range of the Byte type.
// Converted the Int16 value 0 to the Byte value 0.
// Converted the Int16 value 121 to the Byte value 121.
// The Int16 value 340 is outside the range of the Byte type.
// The Int16 value 32767 is outside the range of the Byte type.
// </Snippet3>

let convertInt32 () =
// <Snippet4>
let numbers = [| Int32.MinValue; -1; 0; 121; 340; Int32.MaxValue |]
for number in numbers do
try
let result = Convert.ToByte number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
// The example displays the following output:
// The Int32 value -2147483648 is outside the range of the Byte type.
// The Int32 value -1 is outside the range of the Byte type.
// Converted the Int32 value 0 to the Byte value 0.
// Converted the Int32 value 121 to the Byte value 121.
// The Int32 value 340 is outside the range of the Byte type.
// The Int32 value 2147483647 is outside the range of the Byte type.
// </Snippet4>

let convertInt64 () =
// <Snippet5>
let numbers = [| Int64.MinValue; -1L; 0L; 121L; 34L; Int64.MaxValue |]
for number in numbers do
try
let result = Convert.ToByte number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
// The example displays the following output:
// The Int64 value -9223372036854775808 is outside the range of the Byte type.
// The Int64 value -1 is outside the range of the Byte type.
// Converted the Int64 value 0 to the Byte value 0.
// Converted the Int64 value 121 to the Byte value 121.
// The Int64 value 340 is outside the range of the Byte type.
// The Int64 value 9223372036854775807 is outside the range of the Byte type.
// </Snippet5>

let convertObject () =
// <Snippet6>
let values: obj[] =
[| true; -12; 163; 935; 'x'; "104"; "103.0"
"-1"; "1.00e2"; "One"; 1.00e2 |]
for value in values do
try
let result = Convert.ToByte value
printfn $"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."
with
| :? OverflowException ->
printfn $"The {value.GetType().Name} value {value} is outside the range of the Byte type."
| :? FormatException ->
printfn $"The {value.GetType().Name} value {value} is not in a recognizable format."
| :? InvalidCastException ->
printfn $"No conversion to a Byte exists for the {value.GetType().Name} value {value}."
// The example displays the following output:
// Converted the Boolean value True to the Byte value 1.
// The Int32 value -12 is outside the range of the Byte type.
// Converted the Int32 value 163 to the Byte value 163.
// The Int32 value 935 is outside the range of the Byte type.
// Converted the Char value x to the Byte value 120.
// Converted the String value 104 to the Byte value 104.
// The String value 103.0 is not in a recognizable format.
// The String value -1 is outside the range of the Byte type.
// The String value 1.00e2 is not in a recognizable format.
// The String value One is not in a recognizable format.
// Converted the Double value 100 to the Byte value 100.
// </Snippet6>

let convertSByte () =
// <Snippet7>
let numbers = [| SByte.MinValue; -1y; 0y; 10y; SByte.MaxValue |]
for number in numbers do
try
let result = Convert.ToByte number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
// The example displays the following output:
// The SByte value -128 is outside the range of the Byte type.
// The SByte value -1 is outside the range of the Byte type.
// Converted the SByte value 0 to the Byte value 0.
// Converted the SByte value 10 to the Byte value 10.
// Converted the SByte value 127 to the Byte value 127.
// </Snippet7>

let convertUInt16 () =
// <Snippet8>
let numbers = [| UInt16.MinValue; 121us; 340us; UInt16.MaxValue |]
for number in numbers do
try
let result = Convert.ToByte number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
// The example displays the following output:
// Converted the UInt16 value 0 to the Byte value 0.
// Converted the UInt16 value 121 to the Byte value 121.
// The UInt16 value 340 is outside the range of the Byte type.
// The UInt16 value 65535 is outside the range of the Byte type.
// </Snippet8>

let convertUInt32 () =
// <Snippet9>
let numbers = [| UInt32.MinValue; 121u; 340u; UInt32.MaxValue |]
for number in numbers do
try
let result = Convert.ToByte number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
// The example displays the following output:
// Converted the UInt32 value 0 to the Byte value 0.
// Converted the UInt32 value 121 to the Byte value 121.
// The UInt32 value 340 is outside the range of the Byte type.
// The UInt32 value 4294967295 is outside the range of the Byte type.
// </Snippet9>

let convertUInt64 () =
// <Snippet10>
let numbers= [| UInt64.MinValue; 121uL; 340uL; UInt64.MaxValue |]
for number in numbers do
try
let result = Convert.ToByte number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type."
// The example displays the following output:
// Converted the UInt64 value 0 to the Byte value 0.
// Converted the UInt64 value 121 to the Byte value 121.
// The UInt64 value 340 is outside the range of the Byte type.
// The UInt64 value 18446744073709551615 is outside the range of the Byte type.
// </Snippet10>

convertBoolean ()
printfn "-----"
convertChar ()
printfn "-----"
convertInt16 ()
printfn "-----"
convertInt32 ()
printfn "-----"
convertInt64 ()
printfn "-----"
convertObject ()
printfn "-----"
convertSByte ()
printfn "-----"
convertUInt16 ()
printfn "-----"
convertUInt32 ()
printfn "-----"
convertUInt64 ()
Loading