Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

String to Int and Int to Stri- without using builtin library

Hi all,

            Interestingly this is going to be big post on normal datatype conversion but without using any BUILTIN FUNCTION OR LIBRARIES in .NET.

 

Yes, When we need to convert int-> string or vice versa…we used to use toString(), Parse, ConvertTo. .etc..functions to do this task. But I had this doubt when I was going through MS Interview questions. Yes, In Microsoft, they’ve asked this type of questions .. to test the basic knowledge of core programming….but I ‘ve taken 4 days(part time) to comeout with solutions for this after discussing with my colleagues( Jayveer & Vaibhav).

I’m sure about it technicallity or the way I’ve coded..but not sure about the possibility of doing the same in any other way. If you know something about hardcore conversion or links pls leave it in comment..

 

//Issue Topic: DataType conversion in C# ( How compiler/inbuilt function doing the same…? )

// To convert to data type without using any inbuily function provided by the Framework

//Logic : Compiler Level

//Language used : C#

// CODE Written By : Mahesh

// Courtesy : XOR Algorithm / Idea by  :mladenp

// For String reversing…http://weblogs.sqlteam.com/mladenp/archive/2006/03/19/9350.aspx?Pending=true

 

/////////////////////////// STRING -> INT/////////////////////////////////////////

using System;

namespace Mystring

{

    class Program

    {

        static void Main(string[] args)

        {

            # region STRING -> INT

            string strInput = "300";

            int intOutput = 0;

            for (int i = 0; i < strInput.Length; i++)

            {

                intOutput = intOutput * 10;

                intOutput = intOutput + strInput[i] – 48;

            }

            Console.WriteLine("Input is string ->300 and ouput is Integer->" + intOutput.ToString()); 

            Console.WriteLine("");

            # endregion

/////////////////////////// INT -> STRING/////////////////////////////////////////

            # region INT -> STRING

            int intIndex = 1;

            int intInput = 23456;

            string strResult = "";

            while (intIndex > 0)

            {

                if (intInput == 0)

                    break;

                intIndex = intInput % 10;

                intInput = intInput / 10;

                strResult += intIndex;

            }

 

      //Actual string reverse is happening here

            char[] chr = strResult.ToCharArray();

            int intLen = strResult.Length – 1;

            for (int i = 0; i < intLen; i++, intLen–)

            {

                //using XOR – Refer the XOR Table below

                chr[i] ^= chr[intLen];

                chr[intLen] ^= chr[i];

                chr[i] ^= chr[intLen];

            }

            string strRes = new string(chr);

            Console.WriteLine("Input is Int ->23456 and ouput is String->" + strRes);

            Console.WriteLine("");

        # endregion

        }

    }

}

 

//XOR table

//1 1    0

//0 0    1

//1 0    1

//0 1    1

 

Hope I’ve managed to get the conversion successfully...pls let me know I missed out something or anyother logic to the same..and also feel free to drop ur comment about my expedition on this…

 

Mahesh

mfcmahesh at hotmail.com

Advertisements

May 23, 2006 - Posted by | My .NET Expedition - Revealed

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: