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! ;-)
Right then. What do you propose that someone who has been learning C# for 2 weeks should be doing for problem sheet 1? Implement a FIFO Queue... oh wait, that's already in C#. Implement a linked list? Oh wait, that's there too. I know, implement a multi-threaded chat server!Quote:
Originally Posted by MNovy
If they don't learn how to think about problems and solve stuff like this for themselves, they'll fail. Personally, i don't particularly care. I know someone who had to copy/paste their way through comp sci will never be able to take my job ;)
it is obviously that C# is not that right language for such tasks.
So C# is not the right language for beginners to learn. Grand. That's a fair enough comment. Obviously you believe real programmers should start on C, just like I did (and I'm sure you did too).
Just like you did? And now you don't?Quote:
Originally Posted by Mutant_Fruit
You can only being to learn how to program once in your life. I started on C. Anyway, this is rediculously off topic at this stage. You know how i feel.
One more thing before the OP gets his thread back :)Quote:
Originally Posted by Mutant_Fruit
I'm not sure if you are being sarcastic, or what, haha. But IMHO believe that a starting programmer should start with C, or at least C++. Due to the fact that languages like VB.NET and C# dont show you whats going on under the surface. Sure... C++ doesnt really have the "Instant Gratification" thing going on, but it teaches you about memory, and C teaches you about even lower level things.
I for one can vouch for developers who have never started at a low level, I started with C# right off the bat and said "Wow, this is easy!" and just stayed with it. Low and behold when I became more experienced and started to realize just how nasty the code was that I was writing was getting under the surface, I said "I need to get into some C or assembly and learn about this stuff the right way".
I guess my point being is, new programmers need to be taught the fundamentals, otherwise... with languages like C#, its like having a car but never being able to pop the hood. Just being able to turn the wheel and shift the gears. I wish I would have started in C or C++ first ;)
EDIT: And I dont believe software developers should start and stay on something like C, but I believe its VITAL for them to learn the fundamentals of how memory and computer systems operate.
I'd venture to say that about 90 (- 95)% of development that goes on (and in the business sector it's much higher) does not require knowing what goes on "behind the scenes" as it were to fulfill their responsibilities.
Secondly, I find it amusing to say the least that C++ has become a "low level language." because when C came out, it was considered a high level language because of its abstraction from the underlying assembly / machine languages previously being employed.
IMHO everybody in the world should stay away from C (especially if you plan on being a C++ developer) as it tends to lend itself to bad programming practices from an object oriented standpoint anyway. Unless you plan on sticking to low level OS programming, then I think C is a perfectly fine way to go, otherwise, there are better ways to do things.
Agreed, but I still find comfort in knowing everything I can about how systems operate. In my current job, I was hired in as a C# developer, but I have since then expanded into writing assembly for some of our embedded systems. You never know when you'll need to use it so I find it best to absorb as much as you can :DQuote:
Originally Posted by MadHatter
I guess I classify it as low level just because that's how Ive always thought about it, its an extremely powerful and versatile language. Albeit not a "low level" language by definition...Quote:
Originally Posted by MadHatter
I think most would consider C++ a low level language these days. my data structures teacher was an old retired assembly programmer and didn't think much of C because you didn't have direct access to registers and such. IMO he saw it in the same light that C/C++ programmers may see the likes of java and C#. Not being able to access registers, not being able to manipulate allocations/deletions, access memory directly and so on. I think arguments could me made that you don't need to worry about registers from a C level because the advancement of compilers is good enough to provide an optimal solution, and that with the advancement in JIT compilation you don't need to optimize memory access because again, the compiler can do a better job than most bone headed programmers (think they can).
It's nice that in C you can embed assembly code to optimize some algorithm. Likewise, it's nice that in C# you can use C++/CLI to embed C++(C and asm) into your .net application, and therefore can do a lot of what you may be missing in C#.