to Binary - CSharp System

CSharp examples for System:String Convert

Description

to Binary

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*w w w . j  a v a 2 s. c  o m*/

public class Main{
        public static string toBinary(int i)
        {
            int bin = 524288;
            string result = "";
            while (bin >= 1)
            {
                if (i >= bin)
                {
                    i -= bin;
                    bin /= 2;
                    result += "1";
                }
                else
                {
                    bin /= 2;
                    result += "0";
                }
            }

            if (i == 1) result += "1";

            return result;
        }
}

Related Tutorials