CONVERT A NUMBER FROM BASE2 TO BASE4
//Base 2 to Base 4 with out using extra spacenamespace Base2ToBase4
{
class Program
{
public static void BinaryToBase4(int[] BinArr)
{
if (BinArr == null) return;
int i;
int k;
if (BinArr.Length % 2 == 0)
{
k = i = 0;
}
else
{
k = i = 1;
}
while (i < BinArr.Length)
{
if (BinArr[i] == 0 && BinArr[i + 1] == 0)
{
BinArr[k] = 0;
}
else if (BinArr[i] == 0 && BinArr[i + 1] == 1)
{
BinArr[k] = 1;
}
else if (BinArr[i] == 1 && BinArr[i + 1] == 0)
{
BinArr[k] = 2;
}
else if (BinArr[i] == 1 && BinArr[i + 1] == 1)
{
BinArr[k] = 3;
}
else throw new Exception("Invalid Input");
k++;
i += 2;
}
for (int j = 0; j < k; j++)
Console.Write(BinArr[j]);
}
static void Main(string[] args)
{
int[] binarr = {0,1,1,1,1 };
BinaryToBase4(binarr);
}
}
}
No comments:
Post a Comment