Convert Decimal To Binary

Feb 19, 2012 at 1:02 AM

Is there anyway to Convert Decimal To Binary In Cosmos? Thanks - Matt

Developer
Feb 20, 2012 at 8:16 PM

If BitArrays work (which I'll have to find out) you can do this which would give you a string of ones and zeroes. It isn't really decimal, but an integer should do the job

using System;
using System.Collections;

namespace Cosmos
{
    public static class Number
    {
        public string ToBinary(int number)
        {
            BitArray array = new BitArray(BitConverter.GetBytes(number));
            string s = "";
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i])
                    s += '1';
                else
                    s += '0';
            }
            return s;
        }
    }
}