Java conversions

Last updated Thursday, 28-Jan-1999 03:24:20 PDT by Roedy Green ©1997-1999 Canadian Mind Products.

This table will help you convert from any of the basic data types to any other. Please report any errors or omissions. This table is also available as an amanunesis Applet or as a download with source code.


Convert To
boolean t /*signed*/ byte b /*unsigned*/ byte u short s char c int i long n float f double d String g Boolean tt Character cc Integer ii Long nn Float ff Double dd
From
boolean t -------- // to /*signed*/ byte b from boolean t
b = (byte)(t?1:0);
// to /*unsigned*/ byte u from boolean t
u = (byte)(t?1:0);
// to short s from boolean t
s = (short)(t?1:0);
// to char c from boolean t
c = (char)(t?'1':'0');
/* or */ c = (char)(t?1:0);
// to int i from boolean t
i = t?1:0;
// to long n from boolean t
n = t?1:0;
// to float f from boolean t
f = t?1:0;
// to double d from boolean t
d = t?1:0;
// to String g from boolean t
g = t?"true":"false";
// to Boolean tt from boolean t
tt = new Boolean(t);
// to Character cc from boolean t
cc = new Character((char)(t?'1':'0'));
/* or */ cc = new Character((char)(t?0:1));
// to Integer ii from boolean t
ii = new Integer(t?1:0);
// to Long nn from boolean t
nn = new Long(t?1:0);
// to Float ff from boolean t
ff = new Float(t?1:0);
// to Double dd from boolean t
dd = new Double(t?1:0);
boolean t
/*signed*/ byte b // to boolean t from /*signed*/ byte b
t = b != 0;
-------- // to /*unsigned*/ byte u from /*signed*/ byte b
u = b;
// to short s from /*signed*/ byte b
s = b;
// to char c from /*signed*/ byte b
c = (char)b;
// to int i from /*signed*/ byte b
i = b;
// to long n from /*signed*/ byte b
n = b;
// to float f from /*signed*/ byte b
f = b;
// to double d from /*signed*/ byte b
d = b;
// to String g from /*signed*/ byte b
g = Integer.toString(b);
/* or */ g = Integer.toString(b, 16 /* radix */);
/* or */ g = String.valueOf(b);
/* or */ g = "" + b; /* possibly slow */
// to Boolean tt from /*signed*/ byte b
tt = new Boolean(b != 0);
// to Character cc from /*signed*/ byte b
cc = new Character((char)b);
// to Integer ii from /*signed*/ byte b
ii = new Integer(b);
// to Long nn from /*signed*/ byte b
nn = new Long(b);
// to Float ff from /*signed*/ byte b
ff = new Float(b);
// to Double dd from /*signed*/ byte b
dd = new Double(b);
/*signed*/ byte b
/*unsigned*/ byte u // to boolean t from /*unsigned*/ byte u
t = u != 0;
// to /*signed*/ byte b from /*unsigned*/ byte u
b = u;
-------- // to short s from /*unsigned*/ byte u
s = (short)(u & 0xff);
// to char c from /*unsigned*/ byte u
c = (char)(u & 0xff);
// to int i from /*unsigned*/ byte u
i = u & 0xff;
// to long n from /*unsigned*/ byte u
n = ((long)u) & 0xff;
// to float f from /*unsigned*/ byte u
f = u & 0xff;
// to double d from /*unsigned*/ byte u
d = u & 0xff;
// to String g from /*unsigned*/ byte u
g = Integer.toString(u & 0xff);
/* or */ g = Integer.toString(u & 0xff, 16 /* radix */);
/* or */ g = String.valueOf(u & 0xff);
/* or */ g = "" + (u & 0xff); /* possibly slow */
// to Boolean tt from /*unsigned*/ byte u
tt = new Boolean(u != 0);
// to Character cc from /*unsigned*/ byte u
cc = new Character((char)(u & 0xff));
// to Integer ii from /*unsigned*/ byte u
ii = new Integer(u & 0xff);
// to Long nn from /*unsigned*/ byte u
nn = new Long(((long)u) & 0xff);
// to Float ff from /*unsigned*/ byte u
ff = new Float(((long)u) & 0xff);
// to Double dd from /*unsigned*/ byte u
dd = new Double(((long)u) & 0xff);
/*unsigned*/ byte u
short s // to boolean t from short s
t = s != 0;
// to /*signed*/ byte b from short s
b = (byte)s;
// to /*unsigned*/ byte u from short s
u = (byte)s;
-------- // to char c from short s
c = (char)s;
// to int i from short s
i = s;
// to long n from short s
n = s;
// to float f from short s
f = s;
// to double d from short s
d = s;
// to String g from short s
g = Integer.toString(s);
/* or */ g = Integer.toString(s, 16 /* radix */);
/* or */ g = String.valueOf(s);
/* or */ g = "" + s; /* possibly slow */
// to Boolean tt from short s
tt = new Boolean(s != 0);
// to Character cc from short s
cc = new Character((char)s);
// to Integer ii from short s
ii = new Integer(s);
// to Long nn from short s
nn = new Long(s);
// to Float ff from short s
ff = new Float(s);
// to Double dd from short s
dd = new Double(s);
short s
char c // to boolean t from char c
t = c != 0;
// to /*signed*/ byte b from char c
b = (byte)c;
// to /*unsigned*/ byte u from char c
u = (byte)c;
// to short s from char c
s = (short)(c - '0');
/* or */ s = (short)c;
-------- // to int i from char c
i = c - '0';
/* or */ i = c;
// to long n from char c
n = c - '0';
/* or */ n = c;
// to float f from char c
f = c;
// to double d from char c
d = c;
// to String g from char c
g = String.valueOf(c);
// to Boolean tt from char c
tt = new Boolean(c != '0');
/* or */ tt = new Boolean(c != 0);
// to Character cc from char c
cc = new Character(c);
// to Integer ii from char c
ii = new Integer(c);
// to Long nn from char c
nn = new Long(c);
// to Float ff from char c
ff = new Float(c);
// to Double dd from char c
dd = new Double(c);
char c
int i // to boolean t from int i
t = i != 0;
// to /*signed*/ byte b from int i
b = (byte)i;
// to /*unsigned*/ byte u from int i
u = (byte)i;
// to short s from int i
s = (short)i;
// to char c from int i
c = (char)(i + '0');
/* or */ c = (char)i;
-------- // to long n from int i
n = i;
// to float f from int i
f = i;
// to double d from int i
d = i;
// to String g from int i
g = Integer.toString(i);
/* or */ g = Integer.toString(i, 16 /* radix */);
/* or */ g = String.valueOf(i);
/* or */ g = "" + i; /* possibly slow */
// to Boolean tt from int i
tt = new Boolean(i != 0);
// to Character cc from int i
cc = new Character((char)i);
// to Integer ii from int i
ii = new Integer(i);
// to Long nn from int i
nn = new Long(i);
// to Float ff from int i
ff = new Float(i);
// to Double dd from int i
dd = new Double(i);
int i
long n // to boolean t from long n
t = n != 0;
// to /*signed*/ byte b from long n
b = (byte)n;
// to /*unsigned*/ byte u from long n
u = (byte)n;
// to short s from long n
s = (short)n;
// to char c from long n
c = (char)(n + '0');
/* or */ c = (char)n;
// to int i from long n
i = (int)n;
-------- // to float f from long n
f = n;
// to double d from long n
d = n;
// to String g from long n
g = Long.toString(n);
/* or */ g = Long.toString(n, 16 /* radix */);
/* or */ g = String.valueOf(n);
/* or */ g = "" + n; /* possibly slow */
// to Boolean tt from long n
tt = new Boolean(n != 0);
// to Character cc from long n
cc = new Character((char)n);
// to Integer ii from long n
ii = new Integer((int)n);
// to Long nn from long n
nn = new Long(n);
// to Float ff from long n
ff = new Float(n);
// to Double dd from long n
dd = new Double(n);
long n
float f // to boolean t from float f
t = f != 0;
// to /*signed*/ byte b from float f
b = (byte)f;
// to /*unsigned*/ byte u from float f
u = (byte)f;
// to short s from float f
s = (short)f;
// to char c from float f
c = (char)f;
// to int i from float f
i = (int)f;
/* or */ i = Math.round(f);
/* or */ i = (int)Math.ceil(f);
/* or */ i = (int)Math.floor(f);
// to long n from float f
n = (long)f;
/* or */ n = Math.round(f);
/* or */ n = (long)Math.ceil(f);
/* or */ n = (long)Math.floor(f);
-------- // to double d from float f
d = f;
// to String g from float f
g = Float.toString(f);
/* or */ g = String.valueOf(f);
// to Boolean tt from float f
tt = new Boolean(f != 0);
// to Character cc from float f
cc = new Character((char)f);
// to Integer ii from float f
ii = new Integer((int)f);
// to Long nn from float f
nn = new Long((long)f);
// to Float ff from float f
ff = new Float(f);
// to Double dd from float f
dd = new Double(f);
float f
double d // to boolean t from double d
t = d != 0;
// to /*signed*/ byte b from double d
b = (byte)d;
// to /*unsigned*/ byte u from double d
u = (byte)d;
// to short s from double d
s = (short)d;
// to char c from double d
c = (char)d;
// to int i from double d
i = (int)d;
/* or */ i = (int)Math.round(d);
/* or */ i = (int)Math.ceil(d);
/* or */ i = (int)Math.floor(d);
// to long n from double d
n = (long)d;
/* or */ n = Math.round(d);
/* or */ n = (long)Math.ceil(d);
/* or */ n = (long)Math.floor(d);
// to float f from double d
f = (float)d;
-------- // to String g from double d
g = Double.toString(d);
/* or */ g = String.valueOf(d);
// to Boolean tt from double d
tt = new Boolean(d != 0);
// to Character cc from double d
cc = new Character((char)d);
// to Integer ii from double d
ii = new Integer((int)d);
// to Long nn from double d
nn = new Long((long)d);
// to Float ff from double d
ff = new Float(d);
// to Double dd from double d
dd = new Double(d);
double d
String g // to boolean t from String g
t = new Boolean(g.trim()).booleanValue();
// to /*signed*/ byte b from String g
try {
b = (byte)Integer.parseInt(g.trim());
/* or */ b = (byte)Integer.parseInt(g.trim(), 16 /* radix */);
} catch (NumberFormatException e){}
// to /*unsigned*/ byte u from String g
try {
u = (byte)Integer.parseInt(g.trim());
/* or */ u = (byte)Integer.parseInt(g.trim(), 16 /* radix */);
} catch (NumberFormatException e){}
// to short s from String g
try {
s = (short)Integer.parseInt(g.trim());
/* or */ s = (short)Integer.parseInt(g.trim(), 16 /* radix */);
} catch (NumberFormatException e){}
// to char c from String g
try {
c = (char)Integer.parseInt(g.trim());
/* or */ c = (char)Integer.parseInt(g.trim(), 16 /* radix */);
/* or */ c = g.charAt(0 /* position */);
} catch (NumberFormatException e){}
// to int i from String g
try {
i = Integer.parseInt(g.trim());
/* or */ i = Integer.parseInt(g.trim(), 16 /* radix */);
} catch (NumberFormatException e){}
// to long n from String g
try {
n = Long.parseLong(g.trim());
} catch (NumberFormatException e){}
// to float f from String g
try {
f = Float.valueOf(g.trim()).floatValue();
} catch (NumberFormatException e){}
// to double d from String g
try {
d = Double.valueOf(g.trim()).doubleValue();
} catch (NumberFormatException e){}
-------- // to Boolean tt from String g
tt = new Boolean(g.trim());
/* or */ tt = Boolean.valueOf(g.trim());
// to Character cc from String g
try {
cc = new Character((char)Integer.parseInt(g.trim()));
/* or */ cc = new Character((char)Integer.parseInt(g.trim(), 16 /* radix */));
/* or */ cc = new Character(g.charAt(0 /* position */));
} catch (NumberFormatException e){}
// to Integer ii from String g
try {
ii = Integer.valueOf(g.trim());
/* or */ ii = new Integer(g.trim());
} catch (NumberFormatException e){}
// to Long nn from String g
try {
nn = Long.valueOf(g.trim());
/* or */ nn = new Long(g.trim());
} catch (NumberFormatException e){}
// to Float ff from String g
try {
ff = Float.valueOf(g.trim());
/* or */ ff = new Float(g.trim());
} catch (NumberFormatException e){}
// to Double dd from String g
try {
dd = Double.valueOf(g.trim());
/* or */ dd = new Double(g);
} catch (NumberFormatException e){}
String g
Boolean tt // to boolean t from Boolean tt
t = tt.booleanValue();
// to /*signed*/ byte b from Boolean tt
b = (byte)(tt.booleanValue()?1:0);
// to /*unsigned*/ byte u from Boolean tt
u = (byte)(tt.booleanValue()?1:0);
// to short s from Boolean tt
s = (short)(tt.booleanValue()?1:0);
// to char c from Boolean tt
c = (char)(tt.booleanValue()?'1':'0');
/* or */ c = (char)(tt.booleanValue()?0:1);
// to int i from Boolean tt
i = tt.booleanValue()?1:0;
// to long n from Boolean tt
n = tt.booleanValue()?1:0;
// to float f from Boolean tt
f = tt.booleanValue()?1:0;
// to double d from Boolean tt
d = tt.booleanValue()?1:0;
// to String g from Boolean tt
g = tt.toString();
-------- // to Character cc from Boolean tt
cc = new Character((char)(tt.booleanValue()?'1':'0'));
/* or */ cc = new Character((char)(tt.booleanValue()?1:0));
// to Integer ii from Boolean tt
ii = new Integer(tt.booleanValue()?1:0);
// to Long nn from Boolean tt
nn = new Long(tt.booleanValue()?1:0);
// to Float ff from Boolean tt
ff = new Float(tt.booleanValue()?1:0);
// to Double dd from Boolean tt
dd = new Double(tt.booleanValue()?1:0);
Boolean tt
Character cc // to boolean t from Character cc
t = cc.charValue() != 0;
// to /*signed*/ byte b from Character cc
b = (byte)cc.charValue();
// to /*unsigned*/ byte u from Character cc
u = (byte)cc.charValue();
// to short s from Character cc
s = (short)cc.charValue();
// to char c from Character cc
c = cc.charValue();
// to int i from Character cc
i = cc.charValue();
// to long n from Character cc
n = cc.charValue();
// to float f from Character cc
f = cc.charValue();
// to double d from Character cc
d = cc.charValue();
// to String g from Character cc
g = cc.toString();
// to Boolean tt from Character cc
tt = new Boolean(cc.charValue() != '0');
/* or */ tt = new Boolean(cc.charValue() != 0);
-------- // to Integer ii from Character cc
ii = new Integer(cc.charValue());
// to Long nn from Character cc
nn = new Long(cc.charValue());
// to Float ff from Character cc
ff = new Float(cc.charValue());
// to Double dd from Character cc
dd = new Double(cc.charValue());
Character cc
Integer ii // to boolean t from Integer ii
t = ii.intValue() != 0;
// to /*signed*/ byte b from Integer ii
b = (byte)ii.intValue();
// to /*unsigned*/ byte u from Integer ii
u = (byte)ii.intValue();
// to short s from Integer ii
s = (short)ii.intValue();
// to char c from Integer ii
c = (char)ii.intValue();
// to int i from Integer ii
i = ii.intValue();
// to long n from Integer ii
n = ii.intValue();
// to float f from Integer ii
f = ii.floatValue();
// to double d from Integer ii
d = ii.doubleValue();
// to String g from Integer ii
g = ii.toString();
// to Boolean tt from Integer ii
tt = new Boolean(ii.intValue() != 0);
// to Character cc from Integer ii
cc = new Character((char)ii.intValue());
-------- // to Long nn from Integer ii
nn = new Long(ii.intValue());
// to Float ff from Integer ii
ff = new Float(ii.intValue());
// to Double dd from Integer ii
dd = new Double(ii.intValue());
Integer ii
Long nn // to boolean t from Long nn
t = nn.longValue() != 0;
// to /*signed*/ byte b from Long nn
b = (byte)nn.intValue();
// to /*unsigned*/ byte u from Long nn
u = (byte)nn.intValue();
// to short s from Long nn
s = (short)nn.intValue();
// to char c from Long nn
c = (char)nn.intValue();
// to int i from Long nn
i = nn.intValue();
// to long n from Long nn
n = nn.longValue();
// to float f from Long nn
f = nn.floatValue();
// to double d from Long nn
d = nn.doubleValue();
// to String g from Long nn
g = nn.toString();
// to Boolean tt from Long nn
tt = new Boolean(nn.longValue() != 0);
// to Character cc from Long nn
cc = new Character((char)nn.intValue());
// to Integer ii from Long nn
ii = new Integer(nn.intValue());
-------- // to Float ff from Long nn
ff = new Float(nn.longValue());
// to Double dd from Long nn
dd = new Double(nn.longValue());
Long nn
Float ff // to boolean t from Float ff
t = ff.floatValue() != 0;
// to /*signed*/ byte b from Float ff
b = (byte)ff.intValue();
// to /*unsigned*/ byte u from Float ff
u = (byte)ff.intValue();
// to short s from Float ff
s = (short)ff.intValue();
// to char c from Float ff
c = (char)ff.intValue();
// to int i from Float ff
i = ff.intValue();
// to long n from Float ff
n = ff.longValue();
// to float f from Float ff
f = ff.floatValue();
// to double d from Float ff
d = ff.doubleValue();
// to String g from Float ff
g = ff.toString();
// to Boolean tt from Float ff
tt = new Boolean(ff.floatValue() != 0);
// to Character cc from Float ff
cc = new Character((char)ff.intValue());
// to Integer ii from Float ff
ii = new Integer(ff.intValue());
// to Long nn from Float ff
nn = new Long(ff.longValue());
-------- // to Double dd from Float ff
dd = new Double(ff.floatValue());
Float ff
Double dd // to boolean t from Double dd
t = dd.doubleValue() != 0;
// to /*signed*/ byte b from Double dd
b = (byte)dd.intValue();
// to /*unsigned*/ byte u from Double dd
u = (byte)dd.intValue();
// to short s from Double dd
s = (short)dd.intValue();
// to char c from Double dd
c = (char)dd.intValue();
// to int i from Double dd
i = dd.intValue();
// to long n from Double dd
n = dd.longValue();
// to float f from Double dd
f = dd.floatValue();
// to double d from Double dd
d = dd.doubleValue();
// to String g from Double dd
g = dd.toString();
// to Boolean tt from Double dd
tt = new Boolean(dd.doubleValue() != 0);
// to Character cc from Double dd
cc = new Character((char)dd.intValue());
// to Integer ii from Double dd
ii = new Integer(dd.intValue());
// to Long nn from Double dd
nn = new Long(dd.longValue());
// to Float ff from Double dd
ff = new Float(dd.floatValue());
-------- Double dd
boolean t /*signed*/ byte b /*unsigned*/ byte u short s char c int i long n float f double d String g Boolean tt Character cc Integer ii Long nn Float ff Double dd

I find the lack of symmetry in this table most distressing. The namings remind me of the art of venery practiced in the middle ages as a sort of one-up-manship. It is probably too late now to hope for these conversion names to be rationalised. Perhaps someone will invent a preprocessor so that you can use a simple (xxx)-style cast to convert from anything to anything.

Hexadecimal

The radix feature of the toString and parseInt methods lets you handle hexadecimal numbers. Just set the radix to 16.

StringBuffers

Strings and StringBuffers can be interconverted.

StringBuffer q;
String s;

q = new StringBuffer(s);

s = new String(q);
s = q.toString();

Rounding

Rounding often surprises because fractions like 0.1 cannot be precisely be represented in IEEE floating point format. Often you find yourself having to add tiny numbers just prior to printing to get the desired effects.

Rounding to an integer:

long n = Math.round(d);
double d = Math.rint(d);

Rounding to two decimal places:

long n = Math.round(d*100.); /* keep as "pennies" */
double d = Math.rint(d*100.)/100.;

Left Zeros

The standard conversions give you no leading blanks or leading zeroes. Here is a code snippet to convert an integer to string padded with leading left zeroes. With an obvious modification it would give you lead blanks.

public static String toLZ( int i, int len)
{ // converts integer to left-zero padded string, len chars long.
    String s = Integer.toString(i);
    if (s.length() > len ) return s.substring(0,len);
    else if (s.length() < len)
       // pad on left with zeros
       return "000000000000000000000000000".substring(0,len -
       s.length()) + s;
    else return s;
    } // end toLZ

Formatted Output

JDK 1.0.2 has no function like C's printf that lets you control how many positions and decimal places you want in your output. You have to roll your own.

JDK 1.1 has formatting picture classes such as java.util.DateFormat, SimpleDateFormat and DecimalFormat.

Formatting for 1.0.2 is such a common request, you might attain sainthood if you either implemented the 1.1 methods or wrote a formatting class that gives you all the power of printf without the overhead of parsing strings for % produce a string. e.g.

class Form {
static String display(int value, int width, int decimalPlaces, int base, boolean leadZeroes);
static String display(int value, int width, int decimalPlaces, int base);
static String display(int value, int width, int decimalPlaces);
static String display(int value, int width);
static String display(int value);
static String display(double value, int width, int decimalPlaces);
static String display(double value, int width);
static String display(double value);
... };

broken_linkAcme has written a class to do formatting. They provide source.

Reading Numeric Data From an ASCII file

Java has no built-in methods for reading data of the form: 123 456,-4.

You have to roll your own method. Use java.io.StreamTokenizer or java.util.StringTokenizer, perhaps in combination with readLine to get your data into strings. StreamTokenizer has bells and whistles to deal with parsing source code, including white space, comments and numbers. StringTokenizer just splits the text up based on delimiter characters. Then use the conversion methods in the table above to convert to integers etc. See below for a simplified examples of how you would do this.

StreamTokenizer Method Of Reading Integers From An ASCII File

// Read space or comma-delimited file of integers
// Convert from ASCII to internal binary

import java.io.*;
import java.io.StreamTokenizer;

...

// In JDK 1.1
BufferedReader in =
    new BufferedReader(new FileReader("C:\\temp\\foo.in"));

// in JDK 1.0.2
BufferedInputStream in =
    new BufferedInputStream(new FileInputStream("C:\\temp\\foo.in"));

StreamTokenizer st = new StreamTokenizer(in);
    // process the entire file, of space or comma-delimited ints
    int found = StreamTokenizer.TT_NUMBER;
    while (found != StreamTokenizer.TT_EOF) {
        found = st.nextToken();
        if (found == StreamTokenizer.TT_NUMBER) {
            int i = (int) st.nval;
            System.out.println(i);
            } // end if
    } // end while

StringTokenizer Method Of Reading Integers From An ASCII file

// Read space-delimited file of integers
// Convert from ASCII to internal binary

import java.io.*;
import java.util.StringTokenizer;

...

DataInputStream in = new DataInputStream(
    new BufferedInputStream(new FileInputStream("C:\\temp\\foo.in")));
String aLine = in.readLine();
StringTokenizer st = new StringTokenizer(aLine);
    // process one line worth of space-delimited integers
    while (st.hasMoreTokens()) {
        String s = st.nextToken();
        int i = Integer.parseInt(s);
        System.out.println(i);
        } // end while


CMP_home You are visitor number
1255.
HTML Checked! award
CMP_home Canadian Mind Products You can get an updated copy of this page from http://mindprod.com/converttable.html The Mining Company's
Focus on Java
Best of the Net Award