Initial work to add numbersystems as Extension Methods

This commit is contained in:
Scalpel_cp 2008-04-05 12:52:39 +00:00
parent 6b5e8c1de6
commit cbbf94df3f
5 changed files with 116 additions and 1 deletions

View file

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace FrodeTest.Extension.NumberSystem
{
class Binary
{
}
}

View file

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace FrodeTest.Extension.NumberSystem
{
public static class Hex
{
#region C# 3.0 Extension Methods
public static string AsHex(this int n)
{
return ConvertToHex((UInt32)n);
}
#endregion
#region Converters
private static string ConvertToHex(UInt32 num)
{
string xHex = string.Empty;
while (num >= 16)
{
xHex = (SingleDigitToHex((byte)(num & 0xf))) + xHex;
num = num / 16;
}
xHex = SingleDigitToHex((byte)(num & 0xf)) + xHex;
return xHex;
}
private static char SingleDigitToHex(byte d)
{
switch (d)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 10:
return 'A';
case 11:
return 'B';
case 12:
return 'C';
case 13:
return 'D';
case 14:
return 'E';
case 15:
return 'F';
}
return ' ';
}
#endregion
}
}

View file

@ -49,6 +49,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Debug\SortedListSearcher.cs" />
<Compile Include="Extension\NumberSystem\Binary.cs" />
<Compile Include="Extension\NumberSystem\Hex.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security\User.cs" />
@ -59,6 +61,7 @@
<Compile Include="Test\ExtensionMethodsTest.cs" />
<Compile Include="Test\InterfaceTest.cs" />
<Compile Include="Test\Mock\FakeBroadcastPacket.cs" />
<Compile Include="Test\NumberSystemTest.cs" />
<Compile Include="Test\PacketHeaderTest.cs" />
<Compile Include="Test\RAMBusTest.cs" />
<Compile Include="Test\RTL8139Test.cs" />

View file

@ -25,7 +25,6 @@ namespace FrodeTest
Security.User currentUser = Security.User.Authenticate("frode", "secret");
Shell.Session currentSession = Shell.Session.CreateSession(currentUser);
currentSession.Run();
//Test
//Debug.SortedListSearcher.RunTest();
@ -38,6 +37,7 @@ namespace FrodeTest
//Test.BoolTest.RunTest();
//Test.InterfaceTest.RunTest();
Test.ExtensionMethodsTest.RunTest();
Test.NumberSystemTest.RunTest();
//Done
Console.WriteLine("Shutting down computer");

View file

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrodeTest.Extension.NumberSystem;
namespace FrodeTest.Test
{
class NumberSystemTest
{
public static void RunTest()
{
Console.WriteLine("Testing Numbersystems");
Console.WriteLine("0 as HEX: " + 0.AsHex());
Console.WriteLine("10 as HEX: " + 10.AsHex());
Console.WriteLine("15 as HEX: " + 15.AsHex());
Console.WriteLine("16 as HEX: " + 16.AsHex());
Console.WriteLine("17 as HEX: " + 17.AsHex());
Console.WriteLine("1023 as HEX: " + 1023.AsHex());
Console.WriteLine("99999 as HEX: " + 99999.AsHex());
}
}
}