|
| 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