/* * [Grid14.java] * * Summary: generates Java source code to do conversions for JDK 1.4-. * * Copyright: (c) 2005-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 5.3 2009-03-30 optimise suggested conversion code based on suggestions from Intellij Idea Inspector. * orrect table entries for unsigned conversions that could not be handled purely with autoboxing. */ package com.mindprod.converter; /** * generates Java source code to do conversions for JDK 1.4-. *

* The guts of the conversion process. * Generate sample Java source code for any source/target pair of types. See howregenerate.txt for how to regenerate * matrix files and test for validity. * Future: char[] byte[] String interconversions. add unsigned Byte signed Byte * types *

* * @author Roedy Green, Canadian Mind Products * @version 5.3 2009-03-30 optimise suggested conversion code based on suggestions from Intellij Idea Inspector. * orrect table entries for unsigned conversions that could not be handled purely with autoboxing. * @noinspection PointlessBooleanExpression * @since 1997 */ final class Grid14 { /** * Sample code showing one or more ways to perform each of the 19 x 19 = 361 possible conversions. from=row=0..18 * to=col=0..18. */ static final String[][] how14 = { { /* from boolean */ null, "b = t?(byte)1:(byte)0;", "u = t?(byte)1:(byte)0;", "s = t?(short)1:(short)0;", "// to get '0' and '1'\n" + "c = t?'1':'0';\n" + "// or to get Unicode value 0 or 1\n" + "c = t?(char)1:(char)0;", "i = t?1:0;", "n = t?1L:0L;", "f = t?1.0f:0.0f;", "d = t?1.0d:0.0d;", "g = String.valueOf(t);", "tt = t?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte(t?(byte)1:(byte)0);", "uu = new Byte(t?(byte)1:(byte)0);", "ss = new Short(t?(short)1:(short)0);", "// to get '0' or '1'\n" + "cc = new Character(t?'1':'0');\n" + "// or to get Unicode 0 or 1\n" + "cc = new Character(t?(char)1:(char)0);\n", "// best JDK 1.5+\n" + "ii = Integer.valueOf(t?1:0);\n" + "// or JDK 1.4-\n" + "ii = new Integer(t?1:0);", "// best JDK 1.5+\n" + "nn = Long.valueOf(t?1L:0L);\n" + "// or JDK 1.4-\n" + "nn = new Long(t?1L:0L);", "ff = new Float(t?1.f:0.0f);", "dd = new Double(t?1.0d:0.0d);", }, { /* from signed byte */ "t = b!=0;", null, "u = b;", "s = b;", "// 9 -> '9'\n" + "c = (char)(b + '0');\n" + "// or to get Unicode value\n" + "c = (char)b;", "i = b; // sign extends.", "n = b; // sign extends.", "f = b;", "d = b;", "// best for readability\n" + "g = Integer.toString(b);\n" + "// best for maintainability\n" + "g = String.valueOf(b);\n" + "// or\n" + "g = Integer.toString(b, 7 /* radix */);\n" + "// or\n" + "g = Integer.toBinaryString(b);\n" + "// or\n" + "g = Integer.toOctalString(b);\n" + "// or\n" + "g = Integer.toHexString(b);\n" + "// or kludgy and slow on unoptimised Javas\n" + "g = \"\" + b;", "tt = (b!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte(b);", "uu = new Byte(b);", "ss = new Short(b); // sign extends\n", "// 9 -> '9'\n" + "cc = new Character((char)(b + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)b);", "// best JDK 1.5+\n" + "ii = Integer.valueOf(b);\n" + "// or JDK 1.4-\n" + "ii = new Integer(b); // sign extends.", "// best JDK 1.5+\n" + "nn = Long.valueOf(b);\n" + "// or JDK 1.4-\n" + "nn = new Long(b); // sign extends.", "ff = new Float(b); // sign extends.", "dd = new Double(b); // sign extends.", } , { /* from unsigned byte */ "t = u!=0;", "b = u;", null, "s = (short)(u & 0xff);", "// 9 -> '9'\n" + "c = (char)((u & 0xff) + '0');\n" + "// or to get Unicode value\n" + "c = (char)(u & 0xff);", "i = u & 0xff; // does not sign extend", "n = ((long)u) & 0xff; // does not sign extend.", "f = u & 0xff; // does not sign extend.", "d = u & 0xff; // does not sign extend.", "// best for readability\n" + "g = Integer.toString(u & 0xff);\n" + "// best for maintainability\n" + "g = String.valueOf(u & 0xff);\n" + "// or\n" + "g = Integer.toString(u & 0xff, 7 /* radix */);\n" + "// or\n" + "g = Integer.toBinaryString(u & 0xff);\n" + "// or\n" + "g = Integer.toOctalString(u & 0xff);\n" + "// or\n" + "g = Integer.toHexString(u & 0xff);\n" + "// or kludgy and possibly slow\n" + "g = \"\" + (u & 0xff);", "tt = (u!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte(u);", "uu = new Byte(u);", "ss = new Short((short)(u & 0xff)); // does not sign extend.", "// 9 -> '9'\n" + "cc = new Character((char)((u & 0xff) + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)(u & 0xff));", "// best JDK 1.5+\n" + "ii = Integer.valueOf(u & 0xff);\n" + "// or JDK 1.4-\n" + "ii = new Integer(u & 0xff); // does not sign extend.", "// best JDK 1.5+\n" + "nn = Long.valueOf(((long)u) & 0xff);\n" + "// or JDK 1.4-\n" + "nn = new Long(((long)u) & 0xff); // does not sign extend.", "ff = new Float(((long)u) & 0xff); // does not sign extend.", "dd = new Double(((long)u) & 0xff); // does not sign extend." } , { /* from short */ "t = s!=0;", "b = (byte)s;", "u = (byte)s;", null, "// 9 -> '9'\n" + "c = (char)(s + '0');\n" + "// or to get Unicode value\n" + "c = (char)s;", "i = s;", "n = s;", "f = s;", "d = s;", "// best for readability\n" + "g = Integer.toString(s);\n" + "// best for maintainability\n" + "g = String.valueOf(s);\n" + "// or\n" + "g = Integer.toString(s, 7 /* radix */);\n" + "// or\n" + "g = Integer.toBinaryString(s);\n" + "// or\n" + "g = Integer.toOctalString(s);\n" + "// or\n" + "g = Integer.toHexString(s);\n" + "// or kludgy and possibly slow\n" + "g = \"\" + s;", "tt = (s!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte((byte)s);", "uu = new Byte((byte)s);", "ss = new Short(s);", "// 9 -> '9'\n" + "cc = new Character((char)(s + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)s);", "// best JDK 1.5+\n" + "ii = Integer.valueOf(s);\n" + "// or JDK 1.4-\n" + "ii = new Integer(s);", "// best JDK 1.5+\n" + "nn = Long.valueOf(s);\n" + "// or JDK 1.4-\n" + "nn = new Long(s);", "ff = new Float(s); /* locale-insensitive */", "dd = new Double(s); /* locale-insensitive */", } , { /* from char (unsigned) */ "// to convert '0' or '1'\n" + "t = c!='0';\n" + "// to convert Unicode 0 or 1\n" + "t = c!=0;", "// international\n" + "b = (byte)Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "b = (byte)(c - '0');\n" + "// or to get Unicode value\n" + "b = (byte)c;", "// international\n" + "u = (byte)Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "u = (byte)(c - '0');\n" + "// or to get Unicode value\n" + "u = (byte)c;", "// international\n" + "s = (short)Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "s = (short)(c - '0');\n" + "// or to get Unicode value\n" + "s = (short)c;", null, "// international\n" + "i = Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "i = c - '0';\n" + "// or to get Unicode value\n" + "i = c; // does not sign extend.", "// international\n" + "n = Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "n = c - '0';\n" + "// or to get Unicode value\n" + "n = c; // does not sign extend.", "f = c; // does not sign extend.", "d = c; // does not sign extend.", "g = String.valueOf(c);", "// for '0' or '1'\n" + "tt = (c!='0')?Boolean.TRUE:Boolean.FALSE;\n" + "// or for Unicode 0 or 1\n" + "tt = (c!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte((byte)c);", "uu = new Byte((byte)c);", "// international\n" + "ss = new Short((short)Character.digit(c, 10 /* radix */));\n" + "// fastest '9' -> 9\n" + "ss = new Short((short)(c - '0'));\n" + "// or to get Unicode value\n" + "ss = new Short((short)c);", "cc = new Character(c);", "// international, JDK 1.5+\n" + "ii = Integer.valueOf(Character.digit(c, 10 /* radix */));\n" + "// fastest '9' -> 9, JDK 1.5+\n" + "ii = Integer.valueOf(c - '0');\n" + "// or to get Unicode value JDK 1.5+\n" + "ii = Integer.valueOf(c);\n" + "// international, JDK 1.4-\n" + "ii = new Integer(Character.digit(c, 10 /* radix */));\n" + "// fastest '9' -> 9, JDK 1.4-\n" + "ii = new Integer(c - '0');\n" + "// or to get Unicode value JDK 1.4-\n" + "ii = new Integer(c);", "// international, JDK 1.5+\n" + "nn = Long.valueOf(Character.digit(c, 10 /* radix */));\n" + "// fastest '9' -> 9, JDK 1.5+\n" + "nn = Long.valueOf(c - '0');\n" + "// or to get Unicode value JDK 1.5+\n" + "nn = Long.valueOf(c);" + "// international, JDK 1.4-\n" + "nn = new Long(Character.digit(c, 10 /* radix */));\n" + "// fastest '9' -> 9, JDK 1.4-\n" + "nn = new Long(c - '0');\n" + "// or to get Unicode value JDK 1.4-\n" + "nn = new Long(c);", "ff = new Float(c); // does not sign extend.", "dd = new Double(c); // does not sign extend." } , { /* from int */ "t = i!=0;", "b = (byte)i;", "u = (byte)i;", "s = (short)i;", "// 9 -> '9'\n" + "c = (char)(i + '0');\n" + "// or to get Unicode value\n" + "c = (char)i;", null, "n = i;", "f = i;\n" + "// to construct a float out of IEEE bits\n" + "f = Float.intBitsToFloat(i);", "d = i;", "// best for readability\n" + "g = Integer.toString(i);\n" + "// best for maintainability\n" + "g = String.valueOf(i);\n" + "// or\n" + "g = Integer.toString(i, 7 /* radix */);\n" + "// or\n" + "g = Integer.toBinaryString(i);\n" + "// or\n" + "g = Integer.toOctalString(i);\n" + "// or\n" + "g = Integer.toHexString(i);\n" + "// or kludgy and possibly slow\n" + "g = \"\" + i;", "tt = (i!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte((byte)i);", "uu = new Byte((byte)i);", "ss = new Short((short)i);", "// 9 -> '9'\n" + "cc = new Character((char)(i + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)i);", "// best JDK 1.5+\n" + "ii = Integer.valueOf(i);\n" + "// or JDK 1.4-\n" + "ii = new Integer(i);", "// best JDK 1.5+\n" + "nn = Long.valueOf(i);\n" + "// or JDK 1.4-\n" + "nn = new Long(i);", "ff = new Float(i);", "dd = new Double(i);" } , { /* from long */ "t = n!=0;", "b = (byte)n;", "u = (byte)n;", "s = (short)n;", "// 9 -> '9'\n" + "c = (char)(n + '0');\n" + "// or to get Unicode value\n" + "c = (char)n;", "i = (int)n;", null, "f = n;", "d = n;\n" + "// to construct a double out of IEEE bits\n" + "d = Double.longBitsToDouble (n);", "// best for readability\n" + "g = Long.toString(n);\n" + "// best for maintainability\n" + "g = String.valueOf(n);\n" + "// or\n" + "g = Long.toString(n, 7 /* radix */);\n" + "// or\n" + "g = Long.toBinaryString(n);\n" + "// or\n" + "g = Long.toOctalString(n);\n" + "// or\n" + "g = Long.toHexString(n);\n" + "// or kludgy and possibly slow\n" + "g = \"\" + n;", "tt = (n!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte((byte)n);", "uu = new Byte((byte)n);", "ss = new Short((short)n);", "// 9 -> '9'\n" + "cc = new Character((char)(n + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)n);", "// best JDK 1.5+\n" + "ii = Integer.valueOf((int)n);\n" + "// or JDK 1.4-\n" + "ii = new Integer((int)n);", "// best JDK 1.5+\n" + "nn = Long.valueOf(n);\n" + "// or JDK 1.4-\n" + "nn = new Long(n);", "ff = new Float(n);", "dd = new Double(n);" } , { /* from float */ "t = f!=0;", "b = (byte)f;", "u = (byte)f;", "s = (short)f;", "c = (char)f;", "// best\n" + "i = (int)f;\n" + "// or\n" + "i = Math.round(f);\n" + "// or\n" + "i = (int)Math.ceil(f);\n" + "// or\n" + "i = (int)Math.floor(f);\n" + "// to see the IEEE bits inside a float\n" + "i = Float.floatToIntBits(f);", "// best\n" + "n = (long)f;\n" + "// or\n" + "n = Math.round(f);\n" + "// or\n" + "n = (long)Math.ceil(f);\n" + "// or\n" + "n = (long)Math.floor(f);", null, "d = f;", "// 2 decimal places, rounded, locale-sensitive.\n" + "java.text.DecimalFormat df2\n" + " = new java.text.DecimalFormat(\"###,##0.00\");\n" + "g = df2.format(f);\n" + "// or exponential scientific format, locale-sensitive.\n" + "java.text.DecimalFormat de\n" + " = new java.text.DecimalFormat(\"0.000000E00\");\n" + "g = de.format(f);\n" + "// or best for readability, no loss of precision, locale-insensitive\n" + "g = Float.toString(f);\n" + "// or best for maintainability, no loss of precision, locale-insensitive\n" + "g = String.valueOf(f);\n", "tt = (f!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte((byte)f);", "uu = new Byte((byte)f);", "ss = new Short((short)f);", "cc = new Character((char)f);", "ii = Integer.valueOf((int)f);", "nn = Long.valueOf((long)f);", "ff = new Float(f);", "dd = new Double(f);" } , { /* from double */ "t = d!=0;", "b = (byte)d;", "u = (byte)d;", "s = (short)d;", "c = (char)d;", "// best\n" + "i = (int)d;\n" + "// or\n" + "i = (int)Math.round(d);\n" + "// or\n" + "i = (int)Math.ceil(d);\n" + "// or\n" + "i = (int)Math.floor(d);", "// best\n" + "n = (long)d;\n" + "// or\n" + "n = Math.round(d);\n" + "// or\n" + "n = (long)Math.ceil(d);\n" + "// or\n" + "n = (long)Math.floor(d);\n" + "// to see the IEEE bits inside a double\n" + "n = Double.doubleToLongBits(d);", "f = (float)d;", null, "// 2 decimal places, rounded, locale-sensitive.\n" + "java.text.DecimalFormat df2\n" + " = new java.text.DecimalFormat(\"###,##0.00\");\n" + "g = df2.format(d);\n" + "// or exponential scientific format, locale-sensitive.\n" + "java.text.DecimalFormat de\n" + " = new java.text.DecimalFormat(\"0.0000000000E00\");\n" + "g = de.format(d);\n" + "// or best for readability, no loss of precision, locale-insensitive\n" + "g = Double.toString(d);\n" + "// or best for maintainability, no loss of precision, locale-insensitive\n" + "g = String.valueOf(d);", "tt = (d!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte((byte)d);", "uu = new Byte((byte)d);", "ss = new Short((short)d);", "cc = new Character((char)d);", "ii = Integer.valueOf((int)d);", "nn = Long.valueOf((long)d);", "ff = new Float(d);", "dd = new Double(d);", } , { /* from String */ "t = Boolean.valueOf(g.trim()).booleanValue();\n" + "// or\n" + "t = g.trim().equalsIgnoreCase(\"true\");", "try {\n" + "// best\n" + "b = (byte)Integer.parseInt(g.trim());\n" + "// or\n" + "b = (byte)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best\n" + "u = (byte)Integer.parseInt(g.trim());\n" + "// or\n" + "u = (byte)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best\n" + "s = (short)Integer.parseInt(g.trim());\n" + "// or\n" + "s = (short)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// \"9\" -> '9'\n" + "c = g.charAt(0 /* position */);\n" + "// or to get Unicode value\n" + "c = (char)Integer.parseInt(g.trim());\n" + "// or to get Unicode hex value\n" + "c = (char)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best\n" + "i = Integer.parseInt(g.trim());\n" + "// or\n" + "i = Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "n = Long.parseLong(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best locale-insensitive\n" + "f = Float.parseFloat(g.trim());\n" + "// or locale-insensitive\n" + "f = Float.valueOf(g.trim()).floatValue();\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best, locale-insensitive\n" + "d = Double.parseDouble(g.trim()); \n" + "// or locale-insensitive\n" + "d = Double.valueOf(g.trim()).doubleValue(); \n" + "} catch (NumberFormatException e){/* ... */}", null, "tt = Boolean.valueOf(g.trim());", "try {\n" + "// best\n" + "bb = new Byte(Byte.parseByte(g.trim()));\n" + "// or\n" + "bb = new Byte(Byte.parseByte(g.trim(), 16 /* radix */));\n" + "// or\n" + "bb = new Byte((byte)g.charAt(0 /* position */));\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best\n" + "uu = new Byte(Byte.parseByte(g.trim()));\n" + "// or\n" + "uu = new Byte(Byte.parseByte(g.trim(), 16 /* radix */));\n" + "// or\n" + "uu = new Byte((byte)g.charAt(0 /* position */));\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best\n" + "ss = new Short(Short.parseShort(g.trim()));\n" + "// or\n" + "ss = new Short(Short.parseShort(g.trim(), 16 /* radix */));\n" + "// or\n" + "ss = new Short((short)g.charAt(0 /* position */));\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// \"9\" -> '9'\n" + "cc = new Character(g.charAt(0 /* position */));\n" + "// or to get Unicode value\n" + "cc = new Character((char)Integer.parseInt(g.trim()));\n" + "// or to get Unicode hex value\n" + "cc = new Character((char)Integer.parseInt(g.trim(), 16 /* radix */));\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best, caches\n" + "ii = Integer.valueOf(g.trim());\n" + "// or\n" + "ii = new Integer(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best, caches\n" + "nn = Long.valueOf(g.trim());\n" + "// or\n" + "nn = new Long(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best, locale-insensitive.\n" + "ff = new Float(g.trim());\n" + "// or locale-insensitive\n" + "ff = Float.valueOf(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// best, locale-insensitive*/\n" + "dd = new Double(g); \n" + "// or locale-insensitive\n" + "dd = Double.valueOf(g); \n" + "} catch (NumberFormatException e){/* ... */}", } , { /* from Boolean */ "t = tt.booleanValue();", "b = tt.booleanValue()?(byte)1:(byte)0;", "u = tt.booleanValue()?(byte)1:(byte)0;", "s = tt.booleanValue()?(short)1:(short)0;", "// to get '0' and '1'\n" + "c = tt.booleanValue()?'1':'0';\n" + "// or to get Unicode 0 and 1\n" + "c = tt.booleanValue()?(char)1:(char)0;", "i = tt.booleanValue()?1:0;", "n = tt.booleanValue()?1L:0L;", "f = tt.booleanValue()?1.0f:0.0f;", "d = tt.booleanValue()?1.0d:0.0d;", "g = tt.toString();", null, "bb = new Byte(tt.booleanValue()?(byte)1:(byte)0);", "uu = new Byte(tt.booleanValue()?(byte)1:(byte)0);", "ss = new Short(tt.booleanValue()?(short)1:(short)0);", "// to get '0' and '1'\n" + "cc = new Character(tt.booleanValue()?'1':'0');\n" + "// or to get Unicode 0 or 1\n" + "cc = new Character(tt.booleanValue()?(char)1:(char)0);", "ii = Integer.valueOf(tt.booleanValue()?1:0);", "nn = Long.valueOf(tt.booleanValue()?1L:0L);", "ff = new Float(tt.booleanValue()?1.0f:0.0f);", "dd = new Double(tt.booleanValue()?1.0d:0.0d);" } , { /* from signed Byte */ "t = bb.byteValue()!=0;", "b = bb.byteValue();", "u = bb.byteValue();", "s = bb.shortValue();", "// 9 -> '9'\n" + "c = (char)(bb.byteValue() + '0');\n" + "// or to get Unicode value\n" + "c = (char)bb.byteValue();", "i = bb.intValue(); ", "n = bb.longValue();", "f = bb.floatValue();", "d = bb.doubleValue();", "g = bb.toString();", "// best\n" + "tt = (bb.byteValue()!='0')?Boolean.TRUE:Boolean.FALSE;\n" + "// or\n" + "tt = (bb.byteValue()!=0)?Boolean.TRUE:Boolean.FALSE;", null, "uu = bb;", "ss = new Short(bb.shortValue());", "// 9 -> '9'\n" + "cc = new Character((char)(bb.byteValue() + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)bb.shortValue());", "// best JDK 1.5+\n" + "ii = Integer.valueOf(bb.byteValue());\n" + "// or JDK 1.4-\n" + "ii = new Integer(bb.byteValue());", "// best JDK 1.5+\n" + "nn = Long.valueOf(bb.byteValue());\n" + "// or JDK 1.4-\n" + "nn = new Long(bb.byteValue());", "ff = new Float(bb.byteValue());", "dd = new Double(bb.byteValue());", } , { /* from unsigned Byte */ "t = uu.byteValue()!=0;", "b = uu.byteValue();", "u = uu.byteValue();", "s = (short)(uu.intValue() & 0xff);", "// 9 -> '9'\n" + "c = (char)((uu.intValue() + '0') & 0xff);\n" + "// or to get Unicode value\n" + "c = (char)(uu.intValue() & 0xff);", "i = uu.intValue() & 0xff;", "n = uu.longValue() & 0xff;", "f = (float)(uu.intValue() & 0xff);", "d = (double)(uu.intValue() & 0xff);", "g = uu.toString();", "// best\n" + "tt = (uu.byteValue()!='0')?Boolean.TRUE:Boolean.FALSE;\n" + "// or\n" + "tt = (uu.byteValue()!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = uu;", null, "ss = new Short((short)(uu.byteValue() & 0xff));", "// 9 -> '9'\n" + "cc = new Character((char)((uu.intValue() + '0') & 0xff));\n" + "// or to get Unicode value\n" + "cc = new Character((char)(uu.intValue() & 0xff));", "// best JDK 1.5+\n" + "ii = Integer.valueOf(uu.intValue() & 0xff);\n" + "// or JDK 1.4-\n" + "ii = new Integer(uu.intValue() & 0xff);", "// best JDK 1.5+\n" + "nn = Long.valueOf(uu.longValue() & 0xff);\n" + "// or JDK 1.4-\n" + "nn = new Long(uu.longValue() & 0xff);", "ff = new Float(uu.intValue() & 0xff);", "dd = new Double(uu.intValue() & 0xff);", } , { /* from Short */ "t = ss.shortValue()!=0;", "b = (byte)ss.shortValue();", "u = (byte)ss.shortValue();", "s = ss.shortValue();", "// 9 -> '9'\n" + "c = (char)(ss.shortValue() + '0');\n" + "// or to get Unicode value\n" + "c = (char)ss.shortValue();", "i = ss.intValue(); ", "n = ss.longValue();", "f = ss.floatValue();", "d = ss.doubleValue();", "g = ss.toString();", "// best\n" + "tt = (ss.shortValue()!='0')?Boolean.TRUE:Boolean.FALSE;\n" + "// or\n" + "tt = (ss.shortValue()!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte(ss.byteValue());", "uu = new Byte(ss.byteValue());", null, "// 9 -> '9'\n" + "cc = new Character((char)(ss.shortValue() + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)ss.shortValue());", "// best JDK 1.5+\n" + "ii = Integer.valueOf(ss.shortValue());\n" + "// or JDK 1.4-\n" + "ii = new Integer(ss.shortValue());", "// best JDK 1.5+\n" + "nn = Long.valueOf(ss.shortValue());\n" + "// or JDK 1.4-\n" + "nn = new Long(ss.shortValue());", "ff = new Float(ss.shortValue());", "dd = new Double(ss.shortValue());", } , { /* from Character */ "// to convert '0' or '1'\n" + "t = cc.charValue()!='0';\n" + "// to convert Unicode 0 or 1\n" + "t = cc.charValue()!=0;", "// international\n" + "b = (byte)Character.digit(cc.charValue(), 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "b = (byte)(cc.charValue() - '0');\n" + "// or to get Unicode value\n" + "b = (byte)cc.charValue();", "// international\n" + "u = (byte)Character.digit(cc.charValue(), 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "u = (byte)(cc.charValue() - '0');\n" + "// or to get Unicode value\n" + "u = (byte)cc.charValue();", "// international\n" + "s = (short)Character.digit(cc.charValue(), 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "s = (short)(cc.charValue() - '0');\n" + "// or to get Unicode value\n" + "s = (short)cc.charValue();", "c = cc.charValue();", "// international\n" + "i = Character.digit(cc.charValue(), 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "i = cc.charValue() - '0';\n" + "// or to get Unicode value\n" + "i = cc.charValue(); // does not sign extend.", "// international\n" + "n = Character.digit(cc.charValue(), 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "n = cc.charValue() - '0';\n" + "// or to get Unicode value\n" + "n = cc.charValue(); // does not sign extend.", "f = cc.charValue(); // does not sign extend.", "d = cc.charValue(); // does not sign extend.", "g = cc.toString();", "// for '0' or '1'\n" + "tt = (cc.charValue()!='0')?Boolean.TRUE:Boolean.FALSE;\n" + "// or for Unicode 0 or 1\n" + "tt = (cc.charValue()!=0)?Boolean.TRUE:Boolean.FALSE;", "// international\n" + "bb = new Byte((byte)Character.digit(cc.charValue(), 10 /* radix */));\n" + "// fastest '9' -> 9\n" + "bb = new Byte((byte)(cc.charValue() - '0'));\n" + "// or to get Unicode value\n" + "bb = new Byte((byte)cc.charValue());", "// international\n" + "uu = new Byte((byte)Character.digit(cc.charValue(), 10 /* radix */));\n" + "// fastest '9' -> 9\n" + "uu = new Byte((byte)(cc.charValue() - '0'));\n" + "// or to get Unicode value\n" + "uu = new Byte((byte)cc.charValue());", "// international\n" + "ss = new Short((short)Character.digit(cc.charValue(), 10 /* radix */));\n" + "// fastest '9' -> 9\n" + "ss = new Short((short)(cc.charValue() - '0'));\n" + "// or to get Unicode value\n" + "ss = new Short((short)cc.charValue());", null, "// international, JDK 1.5+\n" + "ii = Integer.valueOf(Character.digit(cc.charValue(), 10 /* radix */));\n" + "// fastest '9' -> 9, JDK 1.5+\n" + "ii = Integer.valueOf(cc.charValue() - '0');\n" + "// or to get Unicode value JDK 1.5+\n" + "ii = Integer.valueOf(cc.charValue());" + "// international, JDK 1.4-\n" + "ii = new Integer(Character.digit(cc.charValue(), 10 /* radix */));\n" + "// fastest '9' -> 9, JDK 1.4-\n" + "ii = new Integer(cc.charValue() - '0');\n" + "// or to get Unicode value JDK 1.4-\n" + "ii = new Integer(cc.charValue());", "// international, JDK 1.5+\n" + "nn = Long.valueOf(Character.digit(cc.charValue(), 10 /* radix */));\n" + "// fastest '9' -> 9, JDK 1.5+\n" + "nn = Long.valueOf(cc.charValue() - '0');\n" + "// or to get Unicode value JDK 1.5+\n" + "nn = Long.valueOf(cc.charValue());" + "// international, JDK 1.4-\n" + "nn = new Long(Character.digit(cc.charValue(), 10 /* radix */));\n" + "// fastest '9' -> 9, JDK 1.4-\n" + "nn = new Long(cc.charValue() - '0');\n" + "// or to get Unicode value JDK 1.4-\n" + "nn = new Long(cc.charValue());", "ff = new Float(cc.charValue()); // does not sign extend.", "dd = new Double(cc.charValue()); // does not sign extend." } , { /* from Integer */ "t = ii.intValue()!=0;", "b = ii.byteValue();", "u = (byte)(ii.byteValue() & 0xff);", "s = ii.shortValue();", "c = (char)ii.intValue();", "i = ii.intValue();", "n = ii.longValue();", "f = ii.floatValue();", "d = ii.doubleValue();", "g = ii.toString();", "tt = (ii.intValue()!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte(ii.byteValue());", "uu = new Byte(ii.byteValue());", "ss = new Short(ii.shortValue());", "// 9 -> '9'\n" + "cc = new Character((char)(ii.intValue() + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)ii.intValue());", null, "// best JDK 1.5+\n" + "nn = Long.valueOf(ii.intValue());\n" + "// or JDK 1.4-\n" + "nn = new Long(ii.longValue());", "ff = new Float(ii.floatValue());", "dd = new Double(ii.doubleValue());", } , { /* from Long */ "t = nn.longValue()!=0;", "b = (byte)nn.intValue();", "u = (byte)nn.intValue();", "s = (short)nn.intValue();", "// 9 -> '9'\n" + "c = (char)(nn.intValue() + '0');\n" + "// or to get Unicode value\n" + "c = (char)nn.intValue();", "i = nn.intValue();", "n = nn.longValue();", "f = nn.floatValue();", "d = nn.doubleValue();", "g = nn.toString();", "tt = (nn.longValue()!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte(nn.byteValue());", "uu = new Byte(nn.byteValue());", "ss = new Short(nn.shortValue());", "// 9 -> '9'\n" + "cc = new Character((char)(nn.intValue() + '0'));\n" + "// or to get Unicode value\n" + "cc = new Character((char)nn.intValue());", "// best JDK 1.5+\n" + "ii = Integer.valueOf(nn.intValue());\n" + "// or JDK 1.4-\n" + "ii = new Integer(nn.intValue());", null, "ff = new Float(nn.longValue());", "dd = new Double(nn.longValue());", } , { /* from Float */ "t = ff.floatValue()!=0;", "b = (byte)ff.intValue();", "u = (byte)ff.intValue();", "s = (short)ff.intValue();", "c = (char)ff.intValue();", "i = ff.intValue();", "n = ff.longValue();", "f = ff.floatValue();", "d = ff.doubleValue();", "// 2 decimal places, rounded, locale-sensitive\n" + "java.text.DecimalFormat df2\n" + " = new java.text.DecimalFormat(\"###,##0.00\");\n" + "g = df2.format(ff.floatValue());\n" + "// or exponential scientific format, locale-sensitive.\n" + "java.text.DecimalFormat de\n" + " = new java.text.DecimalFormat(\"0.000000E00\");\n" + "g = de.format(ff.floatValue()); \n" + "// or best for readability and maintainability, locale-insensitive.\n" + "g = ff.toString();", "tt = (ff.floatValue()!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte(ff.byteValue());", "uu = new Byte(ff.byteValue());", "ss = new Short(ff.shortValue());", "cc = new Character((char)ff.intValue());", "// best JDK 1.5+\n" + "ii = Integer.valueOf(ff.intValue());\n" + "// or JDK 1.4-\n" + "ii = new Integer(ff.intValue());", "// best JDK 1.5+\n" + "nn = Long.valueOf(ff.longValue());\n" + "// or JDK 1.4-\n" + "nn = new Long(ff.longValue());", null, "dd = new Double(ff.floatValue());", } , { /* from Double */ "t = dd.doubleValue()!=0;", "b = (byte)dd.intValue();", "u = (byte)dd.intValue();", "s = (short)dd.intValue();", "c = (char)dd.intValue();", "i = dd.intValue();", "n = dd.longValue();", "f = dd.floatValue();", "d = dd.doubleValue();", "// 2 decimal places, rounded, locale-sensitive\n" + "java.text.DecimalFormat df2\n" + " = new java.text.DecimalFormat(\"###,##0.00\");\n" + "g = df2.format(dd.doubleValue());\n" + "// or exponential scientific format, locale sensitive.\n" + "java.text.DecimalFormat de\n" + " = new java.text.DecimalFormat(\"0.0000000000E00\");\n" + "g = de.format(dd.doubleValue()); \n" + "// or best for readability and maintainability, locale-insensitive\n" + "g = dd.toString();", "tt = (dd.doubleValue()!=0)?Boolean.TRUE:Boolean.FALSE;", "bb = new Byte(dd.byteValue());", "uu = new Byte(dd.byteValue());", "ss = new Short(dd.shortValue());", "cc = new Character((char)dd.intValue());", "// best JDK 1.5+\n" + "ii = Integer.valueOf(dd.intValue());\n" + "// or JDK 1.4-\n" + "ii = new Integer(dd.intValue());", "// best JDK 1.5+\n" + "nn = Long.valueOf(dd.longValue());\n" + "// or JDK 1.4-\n" + "nn = new Long(dd.longValue());", "ff = new Float(dd.floatValue());", null, } }; /** * Hide the constructor. ConvertGrid is purely static. */ private Grid14() { /* statics only */ } } // end Grid15