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

Skip to content

System.Byte F# snippets #7532

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 2 commits into from
Jan 5, 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,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="systembyte.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
open System

//<Snippet1>
let minMaxFields numberToSet =
if numberToSet <= int Byte.MaxValue && numberToSet >= int Byte.MinValue then
// You must explicitly convert an integer to a byte.
let myByte = byte numberToSet

printfn $"The MemberByte value is {myByte}"
else
printfn $"The value {numberToSet} is outside of the range of possible Byte values"

//</Snippet1>

//<Snippet3>
let compare (byte1: byte) byte2 =
let myCompareResult = byte1.CompareTo byte2

if myCompareResult > 0 then
printfn $"{byte1} is less than the MemberByte value {byte2}"

elif myCompareResult < 0 then
printfn $"{byte1} is greater than the MemberByte value {byte2}"

else
printfn $"{byte1} is equal to the MemberByte value {byte2}"
//</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="parse.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
open System
open System.Globalization

let callParse1 () =
// <Snippet1>
let stringToConvert = " 162"
try
let byteValue = Byte.Parse stringToConvert
printfn $"Converted '{stringToConvert}' to {byteValue}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."

// The example displays the following output to the console:
// Converted ' 162' to 162.
// </Snippet1>

let callParse2 () =
// <Snippet2>
let stringToConvert = " 214 "
try
let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {byteValue}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."

let stringToConvert = " + 214 "
try
let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {byteValue}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."

let stringToConvert = " +214 "
try
let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {byteValue}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."

// The example displays the following output to the console:
// Converted ' 214 ' to 214.
// Unable to parse ' + 214 '.
// Converted ' +214 ' to 214.
// </Snippet2>

let callParse3 () =
// <Snippet3>
// Parse value with no styles allowed.
let style = NumberStyles.None
let value = " 241 "
try
let number = Byte.Parse(value, style);
printfn $"Converted '{value}' to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."

// Parse value with trailing sign.
let style = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign
let value = " 163+"
let number = Byte.Parse(value, style)
printfn $"Converted '{value}' to {number}."

// Parse value with leading sign.
let value = " +253 "
let number = Byte.Parse(value, style)
printfn $"Converted '{value}' to {number}."

// This example displays the following output to the console:
// Unable to parse ' 241 '.
// Converted ' 163+' to 163.
// Converted ' +253 ' to 253.
// </Snippet3>

let callParse4 () =
// <Snippet4>
// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
let style = NumberStyles.Float
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
let value = "12,000"

let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."

let culture = CultureInfo.CreateSpecificCulture "en-GB"
try
let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."

let value = "12.000"
let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."

// The example displays the following output to the console:
// Converted '12,000' to 12.
// Unable to parse '12,000'.
// Converted '12.000' to 12.
// </Snippet4>

callParse1 ()
printfn ""
callParse2 ()
printfn ""
callParse3 ()
printfn ""
callParse4 ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
open System
open System.Globalization

let callToString () =
// <Snippet2>
let bytes = [| 0; 1; 14; 168; 255 |]
for byteValue in bytes do
printfn $"{byteValue}"

// The example displays the following output to the console if the current
// culture is en-US:
// 0
// 1
// 14
// 168
// 255
// </Snippet2>

let specifyFormatProvider () =
// <Snippet3>
let bytes = [| 0; 1; 14; 168; 255 |]
let providers =
[ CultureInfo "en-us"
CultureInfo "fr-fr"
CultureInfo "de-de"
CultureInfo "es-es" ]

for byteValue in bytes do
for provider in providers do
printf $"{byteValue.ToString provider,3} ({provider.Name}) "

printfn ""

// The example displays the following output to the console:
// 0 (en-US) 0 (fr-FR) 0 (de-DE) 0 (es-ES)
// 1 (en-US) 1 (fr-FR) 1 (de-DE) 1 (es-ES)
// 14 (en-US) 14 (fr-FR) 14 (de-DE) 14 (es-ES)
// 168 (en-US) 168 (fr-FR) 168 (de-DE) 168 (es-ES)
// 255 (en-US) 255 (fr-FR) 255 (de-DE) 255 (es-ES)
// </Snippet3>

let specifyFormatString () =
// <Snippet4>
let formats =
[ "C3"; "D4"; "e1"; "E2"; "F1"; "G"; "N1"
"P0"; "X4"; "0000.0000" ]
let number = 240uy
for format in formats do
printfn $"'{format}' format specifier: {number.ToString format}"

// The example displays the following output to the console if the
// current culture is en-us:
// 'C3' format specifier: $240.000
// 'D4' format specifier: 0240
// 'e1' format specifier: 2.4e+002
// 'E2' format specifier: 2.40E+002
// 'F1' format specifier: 240.0
// 'G' format specifier: 240
// 'N1' format specifier: 240.0
// 'P0' format specifier: 24,000 %
// 'X4' format specifier: 00F0
// '0000.0000' format specifier: 0240.0000
// </Snippet4>

let formatWithProviders () =
// <Snippet5>
let byteValue = 250uy
let providers =
[ CultureInfo "en-us"
CultureInfo "fr-fr"
CultureInfo "es-es"
CultureInfo "de-de" ]

for provider in providers do
printfn $"""{byteValue.ToString("N2", provider)} ({provider.Name})"""

// The example displays the following output to the console:
// 250.00 (en-US)
// 250,00 (fr-FR)
// 250,00 (es-ES)
// 250,00 (de-DE)
// </Snippet5>

callToString ()
printfn ""
specifyFormatProvider ()
printfn ""
specifyFormatString ()
printfn ""
formatWithProviders ()
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="NewByteMembers.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module TryParse

// <Snippet1>
open System

let callTryParse (stringToConvert: string) =
match Byte.TryParse stringToConvert with
| true, byteValue ->
printfn $"Converted '{stringToConvert}' to {byteValue}"
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."

let byteStrings =
[ null; String.Empty; "1024"
"100.1"; "100"; "+100"; "-100"
"000000000000000100"; "00,100"
" 20 "; "FF"; "0x1F" ]

for byteString in byteStrings do
callTryParse byteString

// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Attempted conversion of '' failed.
// Attempted conversion of '1024' failed.
// Attempted conversion of '100.1' failed.
// Converted '100' to 100
// Converted '+100' to 100
// Attempted conversion of '-100' failed.
// Converted '000000000000000100' to 100
// Attempted conversion of '00,100' failed.
// Converted ' 20 ' to 20
// Attempted conversion of 'FF' failed.
// Attempted conversion of '0x1F' failed.
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module TryParse2

// <Snippet2>
open System
open System.Globalization

let callTryParse (stringToConvert: string) (styles: NumberStyles) =
match Byte.TryParse(stringToConvert, styles, null) with
| true, byteValue ->
printfn $"Converted '{stringToConvert}' to {byteValue}"
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."

[<EntryPoint>]
let main _ =
let byteString = "1024"
let styles = NumberStyles.Integer
callTryParse byteString styles

let byteString = "100.1"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse byteString styles

let byteString = "100.0"
callTryParse byteString styles

let byteString = "+100"
let styles = NumberStyles.Integer ||| NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign
callTryParse byteString styles

let byteString = "-100"
callTryParse byteString styles

let byteString = "000000000000000100"
callTryParse byteString styles

let byteString = "00,100"
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
callTryParse byteString styles

let byteString = "2E+3 "
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse byteString styles

let byteString = "FF"
let styles = NumberStyles.HexNumber
callTryParse byteString styles

let byteString = "0x1F"
callTryParse byteString styles

0

// The example displays the following output to the console:
// Attempted conversion of '1024' failed.
// Attempted conversion of '100.1' failed.
// Converted '100.0' to 100
// Converted '+100' to 100
// Attempted conversion of '-100' failed.
// Converted '000000000000000100' to 100
// Converted '00,100' to 100
// Attempted conversion of '2E+3 ' failed.
// Converted 'FF' to 255
// Attempted conversion of '0x1F' failed.
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="TryParse.fs" />
<Compile Include="TryParse2.fs" />
</ItemGroup>
</Project>
Loading