Hi Guys..
I Wanted to convert an int to a string in C#...but i cannot use any built in functions.So, can any one tell me an algorithm or a program to do so....please is urgent....
Printable View
Hi Guys..
I Wanted to convert an int to a string in C#...but i cannot use any built in functions.So, can any one tell me an algorithm or a program to do so....please is urgent....
you cant. just use myInt.ToString()
Sounds like Problem Sheet 1 of 1st year computer science.
Ask your classmates, or ask your professor for help.
Totally, I always HATED those kinds of problems. Especially when they ask you to do it in a language like C# that already has those types of conversions easily made with the call of ToString().Quote:
Originally Posted by Mutant_Fruit
I think if they want you to learn how its done on the inside, teach C or C++, but if they want you to learn how its done in the real world with languages like C# or VB.NET, teach them the benefits of using functions and members that are already provided in those languages.
Because nobody manually converts a numerical value to a string in C#, not anyone that I know of... The only way I think it can be done is by converting the integer into bytes, and then converting the bytes into a string. But that STILL requires built in encoding methods. So without the use of pointers, its nigh impossible in C#.
The point of the exercise is to teach you to think. Why else would you want someone to implement a list, or a queue. It's just an easy (ish) practice problem. The only way to get good is to program, so give em something simple to do.Quote:
Originally Posted by RaleTheBlade
There's *much* easier ways to do it. It can be done in 8 lines of code including getting the input number and printing the result to screen. No pointers needed. Of course, it's up to the original poster to come up with that solution ;)Quote:
The only way I think it can be done is by converting the integer into a bytes, and then converting the bytes into strings. But that STILL requires built in encoding methods. So without the use of pointers, its nigh impossible in C#
EDIT: If you converted the integer into bytes and converted bytes to string, you'd have to use a HEX representation or do some fairly nasty bit-mangling ;)
As mentioned earlier, you could use the int.ToString() or Convert.ToString(int) methods.
Here's my own method to do that:
Code:IntToString(int n)
{
StringBuilder sb;
string result = null;
if(n == 0)
return "0";
using(sb = new StringBuilder())
{
if(n < 0)
sb.Append('-');
while(n != 0)
{
sb.Append((char)(n%10 + 48));
n /= 10;
}
sb.Reverse();
result = sb.ToString();
}
return result;
}
And now we have another compsci student who has learnt the most important technique for solving problems, the art of Copy&Paste.
technically speaking, that's still using built in functionality...
1.ToString();
2.ToString();
weird!!! it works also because all types inherits from Obejct that it has ToString() method....so you see ToString() method is everywhere in c# even in the hole of a pin or under the leaves of a bean!
C# is completely an Object Oriented Language and strongly typed.
.ToString() is built-in functionality....therefore, it does not apply to his problem.Quote:
Originally Posted by toraj58
I know my answer was complementary of others; my friend!Quote:
Originally Posted by eclipsed4utoo
Algorithm may be something like this
1- Get every digit of the input integer
2- Add the above digit in some char[]
3- Use typecasting for converting int (digit) to char (for char[])
4- When all digits are finished, your resulting string i.e char[] is ready.
Such school tasks are senseless like hell, especially for C#.
Instead learning how to code and implement clean .NET apps,
teachers force students to do such crap. Why the hell
the programming languages are so powerful?!
It has nothing to do with programming skills to be able to code
an IntToString by your own... it is sick. Such tasks are only useful if you are using pure assembler.
...but this is C#... heeeelllloooooo?????
There are billions of other problems to solve than such crap.
Ohhh wait..... why the teacher does not force to build our own cars?! *dough!*
Note for teachers. These are some example which can be implemented:
- IntToBinaryString
- BinayStringToInt
- Calculating slopes (y=mx+b)
- Formula parser
....
It isn't useless to solve such a task for the reason which Mutant Fruit has pointed out. It is useless to suck the solution from others instead to solve it itself, and it is useless to discourage student with talks about useless of learning trought solving problem.
Life is hard.Quote:
and it is useless to discourage student with talks about useless of learning trought solving problem
Welcome to the real world! ;-)