Skip to content

Commit

Permalink
Extension for int for different number systems.
Browse files Browse the repository at this point in the history
Will be used later.
  • Loading branch information
LoneWandererProductions committed Nov 24, 2023
1 parent 51bccb7 commit f26b659
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ExtendedSystemObjects/ExtendedInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

// ReSharper disable UnusedMember.Global

using System;

namespace ExtendedSystemObjects
{
/// <summary>
Expand All @@ -26,5 +28,51 @@ public static bool Interval(this int i, int value, int interval)
{
return i - interval <= value && value <= i + interval;
}

/// <summary>
/// Converts to binary.
/// </summary>
/// <param name="i">The i.</param>
/// <returns>int as binary string</returns>
public static string ToBinary(this int i)
{
//2 is the new base, in our case binary, from decimal
return Convert.ToString(i, 2);
}

/// <summary>
/// Binaries to int.
/// </summary>
/// <param name="binary">The binary.</param>
/// <returns>a binary string to decimal</returns>
public static int BinaryToInt(this string binary)
{
//2 was the base, convert back to decimal
return Convert.ToInt32(binary, 2);
}

/// <summary>
/// Converts to base.
/// </summary>
/// <param name="i">The i.</param>
/// <param name="toBase">To base.</param>
/// <returns>int as target number string</returns>
public static string ToBase(this int i, int toBase)
{
//the toBase is the new base for our number system, from decimal
return Convert.ToString(i, toBase);
}

/// <summary>
/// Binaries to base.
/// </summary>
/// <param name="binary">The binary.</param>
/// <param name="toBase">To base.</param>
/// <returns>a binary string to target number system</returns>
public static int BinaryToBase(this string binary, int toBase)
{
//2 was the base, convert back to base
return Convert.ToInt32(binary, toBase);
}
}
}

0 comments on commit f26b659

Please sign in to comment.