convert an int to a string without built in functions

  • Thread starter Thread starter sagar
  • Start date Start date
S

sagar

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....
 
sagar said:
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....

When asking a question like this, you should explain up front that it is
a homework assignment, so that anyone replying to it can assist you at
an appropriate level.

Strictly speaking what you are asking for is not possible. You have to
use some built in functions to create any string at all. I assume that
you at least may use the functions for converting a character into a
string and concatenating strings, or creating a string from an array of
characters.

As the point of the excercise is to do what the ToString method does, I
assume that you may not use that method at all. Therefore you will have
to convert a value between 0 to 9 into a character by creating the
character value from the character code. The character code for the
digit 0 is 48, so you just add 48 to the value and cast it to a char.

To get the values for the separate digits, you use the modulo operator
(%) and the division operator (/).

You can either convert each character to a string and concatenate the
strings, or put the characters in a character array (char[]) and create
a string from the array.
 
can convert an integer to a string not using any build-in function !
(anyone has something more crazy ? :-) )

I would say create a database table with 2 columns, the first an
integer column and the second the string representation. Then
populate the table with every possible integer. Then use ADO to
lookup the correct string based on the integer passed in!! :p

:)

Chris
 
OD said:
you forgot another solution : writing a web service that makes the call
the Tostring and call this service from the application, so the latter
can convert an integer to a string not using any build-in function !

Well, that would convert the integer to a string to put it in the SOAP
message, then on the other side it would parse the SOAP message to
extract the string, parse the string back into an integer, before you
can convert it to a string, which is then put in a SOAP message, which
is then parsed on the recieving end to extract the string.

So, technically the application is converting the integer to a string
anyway...
(anyone has something more crazy ? :-) )

How about purchasing a few consecutive C nets of IP addresses, so that
you can set up a range of web servers? Add the value of the lowest IP
address as offset to the integer to calculate a new IP address. When
requesting a page using the IP address, each web server would respond
with the string format of the number used to calculate the IP adress.

(However, as an URL is a string, the application would still have to do
some conversion, only not converting the number itself...)

:)
 
Back
Top