Skip to content

Commit 061edcf

Browse files
authored
Add Convert.ToByte(bool) (#135)
***NO_CI***
1 parent 20b17a7 commit 061edcf

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Tests/NFUnitTestConversions/UnitTestConvertTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,5 +357,17 @@ public void Convert_64ParsePerf()
357357
Assert.Equal(val, -7446744073709551615L);
358358
}
359359

360+
[TestMethod]
361+
public void Convert_FromBoolean()
362+
{
363+
bool iamTrue = true;
364+
bool iamFalse = false;
365+
366+
byte convTrueIsOne = Convert.ToByte(iamTrue);
367+
byte convFalseIsZero = Convert.ToByte(iamFalse);
368+
369+
Assert.Equals(convTrueIsOne, 1);
370+
Assert.Equals(convFalseIsZero, 0);
371+
}
360372
}
361373
}

nanoFramework.CoreLibrary/System/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
[assembly: AssemblyProduct(".NET nanoFramework mscorlib")]
1414
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")]
1515

16-
[assembly: AssemblyNativeVersion("100.5.0.6")]
16+
[assembly: AssemblyNativeVersion("100.5.0.7")]

nanoFramework.CoreLibrary/System/Convert.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static class Convert
3636

3737
[MethodImpl(MethodImplOptions.InternalCall)]
3838
private static extern double NativeToDouble(string value);
39+
3940
/// <summary>
4041
/// Converts the value of the specified 8-bit unsigned integer to an equivalent Boolean value.
4142
/// </summary>
@@ -92,6 +93,16 @@ public static byte ToByte(string value, int fromBase = 10)
9293
return (byte)NativeToInt64(value.Trim(), false, Byte.MinValue, Byte.MaxValue, fromBase);
9394
}
9495

96+
/// <summary>
97+
/// Converts the specified <see cref="Boolean"/> value to the equivalent 8-bit unsigned integer.
98+
/// </summary>
99+
/// <param name="value">The <see cref="Boolean"/> value to convert.</param>
100+
/// <returns>The number 1 if <paramref name="value"/> is <see langword="true"/>; otherwise, 0.</returns>
101+
public static byte ToByte(bool value)
102+
{
103+
return value ? (byte)1 : (byte)0;
104+
}
105+
95106
/// <summary>
96107
/// Converts the specified string representation of a number to an equivalent 16-bit signed integer.
97108
/// </summary>

0 commit comments

Comments
 (0)