Confusion on implementation of String class?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Confusion on implementation of String class?

Henoktes722
In Figure 12.4 and 12.5, what are we doing there?

Like there is a method int2String(n) {} // Does this method change ASCII to character?

I don't know what those methods are trying to implement?
Reply | Threaded
Open this post in threaded view
|

Re: Confusion on implementation of String class?

WBahn
Administrator
Look at the comment directly above "int2String(n):" where is says, "Convert a non-negative number to a string".

That's what that function does.

Remember that we don't print numbers to the screen, we print characters.

So if we want to print 257 to the screen, we have to first print the character that looks like '2', followed by the character that looks like '5', followed by the character that looks like '7'.

In memory, the integer 257 is '0000000100000001' (in binary).

From this, we need to get the character codes for '2' (which is the integer 50), '5' (which is the integer 53), and '7' (which is the integer 55).

The int2String function returns a string containing these character codes.
Reply | Threaded
Open this post in threaded view
|

Re: Confusion on implementation of String class?

Henoktes722
Okay Thank you wbahn.

So will int2String(12), return ['1','2'] or [49, 50] ?
Reply | Threaded
Open this post in threaded view
|

Re: Confusion on implementation of String class?

WBahn
Administrator
Keep in mind that those figures are just pseudocode for a couple of possible algorithms on how to perform the conversions.

What you are implementing are the library methods (in the String library) intValue() and setInt(). In both cases (since these are methods) you have a String object that you are either getting the ASCII codes from or storing the ASCII codes to.