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:
Java:
String strnum = "123123.123";
double decnum = Double.parseDouble(strnum);
System.out.println(decnum + 100);


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:
Java:
long decnum = Long.parseLong("rizzix", 36);
System.out.println(decnum + 100);



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:
Java:
String num = ((Integer) 23987).toString();

(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:
Java:
String num = 23123 + "";
(yes concatenate the number with an empty string. Since it's so commonly used, i'm sure this is optimized by the jvm, well at least the recent ones)

The Static Call method:
Java:
String num = Integer.toString(123213);

(this will also work for all Number_Type(s), and is the prefered way as compared to the Casting method)

The String valueOf method:
Java:
String num = String.valueOf(dnum);

(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:
Java:
String num = Integer.toHexString(123235);

(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:
Java:
String num = Integer.toString(123213, 36);

(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.

Java:
class bin2char {
    public static void main(String[] args) {       
        for (int i = 0; i < args.length; i++)
            System.out.print((char) Integer.parseInt(args[i], 2));
        System.out.println();
    }
}


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:
code:
java bin2char 1101001 100000 1100101 1100001 1110100 100000 1100011 1100001 1110100 1110011


hmm Confused

Author:  rizzix [ Sun Oct 31, 2004 10:51 am ]
Post subject: 

and here's the inverse:
Java:
class char2bin {
    public static void main(String[] args) {
        final String SPACE = Integer.toString((int) ' ', 2);
        for (int i = 0, j = 0; i < args.length; i++, j = 0) {
            for (char[] s = args[i].toCharArray(); j < s.length; j++)
                System.out.print(Integer.toString((int) s[j], 2) + " ");
            System.out.print(i == args.length - 1 ? "\n" : SPACE + " ");
        }
    }
}


: