|
-
September 19th, 2008, 10:44 AM
#1
Converting int to string
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....
-
September 19th, 2008, 10:59 AM
#2
Re: Converting int to string
you cant. just use myInt.ToString()
-
September 19th, 2008, 01:23 PM
#3
Re: Converting int to string
Sounds like Problem Sheet 1 of 1st year computer science.
Ask your classmates, or ask your professor for help.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
September 19th, 2008, 03:37 PM
#4
Re: Converting int to string
 Originally Posted by Mutant_Fruit
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().
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#.
Last edited by RaleTheBlade; September 19th, 2008 at 03:41 PM.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
September 19th, 2008, 03:44 PM
#5
Re: Converting int to string
 Originally Posted by RaleTheBlade
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().
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.
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#
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 
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
Last edited by Mutant_Fruit; September 19th, 2008 at 03:47 PM.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
September 21st, 2008, 06:55 AM
#6
Re: Converting int to string
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;
}
-
September 21st, 2008, 07:04 AM
#7
Re: Converting int to string
And now we have another compsci student who has learnt the most important technique for solving problems, the art of Copy&Paste.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
September 21st, 2008, 07:56 AM
#8
Re: Converting int to string
technically speaking, that's still using built in functionality...
-
September 21st, 2008, 08:47 AM
#9
Re: Converting int to string
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.
-
September 21st, 2008, 08:57 AM
#10
Re: Converting int to string
 Originally Posted by toraj58
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.
-
September 21st, 2008, 09:10 AM
#11
Re: Converting int to string
 Originally Posted by eclipsed4utoo
.ToString() is built-in functionality....therefore, it does not apply to his problem.
I know my answer was complementary of others; my friend!
-
September 21st, 2008, 10:52 PM
#12
Re: Converting int to string
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.
-
September 22nd, 2008, 12:19 AM
#13
Re: Converting int to string
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
....
-
September 22nd, 2008, 03:37 AM
#14
Re: Converting int to string
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
September 22nd, 2008, 04:00 AM
#15
Re: Converting int to string
and it is useless to discourage student with talks about useless of learning trought solving problem
Life is hard.
Welcome to the real world! ;-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|