Wednesday, September 5, 2007

Conversions In Java

String to Conversions:

String to boolean conversion:

t = new Boolean(g.trim()).booleanValue();

String to signed byte b

// 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){}

String to unsigned byte

// 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){}

String to Integer Conversion

// to int i from String g
try { i = Integer.parseInt(g.trim()); /* or */
i = Integer.parseInt(g.trim(), 16 /* radix */); }
catch (NumberFormatException e){}

String to Long Conversion

// to long n from String g
try { n = Long.parseLong(g.trim()); }
catch (NumberFormatException e){}

String to float conversion

try
{ f = Float.valueOf(g.trim()).floatValue(); }
catch (NumberFormatException e){}

String to Double Conversion

try
{ d = Double.valueOf(g.trim()).doubleValue(); }
catch (NumberFormatException e){}

No comments: