Computer Science Canada [Tutorial] Conversions: Number <=> String |
Author: | rizzix [ Sun Oct 31, 2004 1:04 am ] | ||||||||||||||||
Post subject: | [Tutorial] Conversions: Number <=> String | ||||||||||||||||
This is a rather obvious tutorial, but go ahead and read it cuz there could be something that you might not have known was possible. All conversion from a String to a number is as follows. Number_Type.parseNumber_Type(the_string_representation_of_the_number); (where Number_Type is one of Double, Float, Long, Short or Integer) example:
The Long, Short and the Integer class also have another variation of the parseNumber_Type function that accepts another argument that is the radix of the string_representation_of_the_number. The maximum radix is defined in the constant Character.MAX_RADIX, in most cases it should be 36. i.e all the character 0-9 and a to z, case insensitive. example:
To convert an number to a String is also quite simple and there are a number of ways you can go about it. The Casting method:
(you cast a number as it's class type and call the toString() method, this will work for all Number_Type(s), just make sure you replace Integer with the correct Number_Type) There is a shortcut to this method that is commonly used:
The Static Call method:
(this will also work for all Number_Type(s), and is the prefered way as compared to the Casting method) The String valueOf method:
(where dnum is any four of int, long, float & double. This is basically a Static Call conversion for you based on the type of "dnum". This is usefull when you want to use one single method-name for different types of numbers) The toBase method:
(there are three different bases you can use: Ocatal, Hex and Binary. Not all of them are implemented in all Number_Type classes, i.e only some of them will work with a perticular type of number. Check out the documentation for this.) The Extended Static Call method:
(this extended version of the Static Call method add an extra argument, i.e the radix. It returns the string representation of the given number (123213) in the given radix (36). It is only implemented in the Integer, Short and Long classes hence will not work with the other two.) |
Author: | rizzix [ Sun Oct 31, 2004 9:08 am ] | ||||
Post subject: | |||||
so lets put all this to some use... we'll create a program that will read a binary sequence of numbers seperated by space and translate it into an ascii sequence of characters.
complie the program. and now all we need is a binary sequence of number.. pfft to lazy to think.. Lets simply decrypt Catalyst's signature, shall we? so run the program likewise:
hmm |
Author: | rizzix [ Sun Oct 31, 2004 10:51 am ] | ||
Post subject: | |||
and here's the inverse:
|