Conversion utilities : Data Type cast « Data Type « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Data Type » Data Type castScreenshots 
Conversion utilities
  
/* 
 * Copyright (c) 2002,2003 Free Software Foundation
 * developed under the custody of the
 * Open Web Application Security Project
 * (http://www.owasp.org)
 
 * This file is part of the OWASP common library (OCL).
 * OCL is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 
 * OCL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 
 * The valid license text for this file can be retrieved with
 * the call:   java -cp owasp.jar org.owasp.LICENSE
 
 * If you are not able to view the LICENSE that way, which should
 * always be possible within a valid and working OCL release,
 * please write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * to get a copy of the GNU General Public License or to report a
 * possible license violation.
 */

import java.util.Arrays;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/** 
 * Conversion utilities.
 
 @since beta 1
 @version beta 1<br />CVS $Revision: 1.8 $ $Author: istr $
 @author <a href="mailto:ingo@ingostruck.de">ingo@ingostruck.de</a>
 */
public final class Convert {
  /** Revision identifier */
  public static final String MODULE = "<Convert beta 1, CVS $Revision: 1.8 $>";
  /** to mimic sizeof(short) = short length in bytes = 12 (=16bit) */
  public static final int SIZEOF_SHORT = 2;
  /** to mimic sizeof(int) = int length in bytes = 4 (=32bit) */
  public static final int SIZEOF_INT = 4;
  /** to mimic sizeof(long) = long length in bytes = 8 (=64bit) */
  public static final int SIZEOF_LONG = 8;
  /** bit length of byte */
  public static final int BITS_BYTE = 8;
  /** bit length of short */
  public static final int BITS_SHORT = BITS_BYTE * SIZEOF_SHORT;
  /** bit length of int */
  public static final int BITS_INT = BITS_BYTE * SIZEOF_INT;
  /** bit length of long */
  public static final int BITS_LONG = BITS_BYTE * SIZEOF_LONG;
  /** mask a byte in an int */
  public static final int MASK_BYTE = 0xFF;
  
  /** Avoids instantiation */
  private Convert () {}

  /**
   * Converts the SIZEOF_LONG bytes starting at off within b to a long value.
   * <br />NOTE: the conversion treats the leftmost byte as the lowest byte of
   * the resulting long value
   @param b a byte[] containing bytes to be converted
   @param off the offset where to find the bytes to be converted
   @return a long value
   @throws IndexOutOfBoundsException if an index violation occurs
   */
  public static long toLong byte[] b, int off ) {
    long r = 0;
    for int i = SIZEOF_LONG - 1; i >= 0; i-- 
      r |= (((longboff + i ]) & MASK_BYTE<< (i*BITS_BYTE);
    return r;
  }

  /**
   * Converts the SIZEOF_INT bytes starting at off within b to an int value.
   * <br />NOTE: the conversion treats the leftmost byte as the lowest byte of
   * the resulting int value
   @param b a byte[] containing bytes to be converted
   @param off the offset where to find the bytes to be converted
   @return an int value
   @throws IndexOutOfBoundsException if an index violation occurs
   */
  public static int toInt byte[] b, int off ) {
    int r = 0;
    for int i = SIZEOF_INT - 1; i >= 0; i-- 
      r |= ((int) (boff + i & MASK_BYTE)) << (i*BITS_BYTE);
    return r;
  }

  /**
   * Converts a long value to SIZEOF_LONG bytes stored in b starting at off.
   * <br />NOTE: the conversion stores the lowest byte of the long value as
   * the leftmost byte within the sequence
   @param val a long value to be split up into bytes
   @param b a byte[] to be written to
   @param off the offset where to start writing within b
   @throws IndexOutOfBoundsException if an index violation occurs
   */
  public static void toBytes long val, byte[] b, int off ) {
    for int i = 0; i < SIZEOF_LONG; i++, val >>= BITS_BYTE 
      boff + i (byte) (val & MASK_BYTE);
  }
  
  /**
   * Converts an int value to SIZEOF_INT bytes stored in b starting at off.
   * <br />NOTE: the conversion stores the lowest byte of the int value as
   * the leftmost byte within the sequence
   @param val an int value to be split up into bytes
   @param b a byte[] to be written to
   @param off the offset where to start writing within b
   @throws IndexOutOfBoundsException if an index violation occurs
   */
  public static void toBytes int val, byte[] b, int off ) {
    for int i = 0; i < SIZEOF_INT; i++, val >>= BITS_BYTE 
      boff + i (byte) (val & MASK_BYTE);
  }
  /**
   * Converts a byte value to a hexadecimal String.
   @param b a byte value
   @return a hexadecimal (upper-case-based) String representation of the
   * byte value
   */
  public static String toHexString byte ) {
    int len = 2;
    byte[] dig = new bytelen ];
    dig(byte) ((b & 0xF0>> 4);
    dig(byte) (b & 0x0F);
    for int i = 0; i < len; i++ )
      dig+= 10 > dig48 55;
    return new Stringdig );
  }

  public static String toHexString byte[] ) {
    if null == b )
      return null;
    int len = b.length;
    byte[] hex = new bytelen << ];
    for int i = 0, j = 0; i < len; i++, j+=) {
      hex(byte) ((b0xF0>> 4);
      hex+= 10 > hex48 55;
      hexj + (byte) (b0x0F)
      hexj + += 10 > hexj + 48 55;
    }
    return new Stringhex );
  }

  /** 
   * Parses a number from a string.
   * Finds the first recognizable base-10 number (integer or floating point)
   * in the string and returns it as a Number.
   @param string String to parse
   @return first recognizable number
   @exception NumberFormatException if no recognizable number is found
   */
  public static Number toNumber String s )
    throws NumberFormatException
  {
    // parsing states
    int INT = 0;
    int FRAC = 1;
    int EXP = 2;
    int p = 0;
    for int i = 0; i < s.length(); ++i ) {
      char c = s.charAt);
      if Character.isDigit) ) {
        int start = i;
        int end = ++i;
        int state = INT;
        if start > && s.charAtstart - == '.' ) {
          --start;
          state = FRAC;
        }
        if start > && s.charAtstart - == '-' )
          --start;
        boolean atEnd = false;
        while !atEnd && i < s.length() ) {
          switch s.charAt) ) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
              end = ++i;
              break;
            case '.':
              if state == INT ) {
                state = FRAC;
                ++i;
              else {
                atEnd = true;
              }
              break;
            case 'e':
            case 'E':
              state = EXP;
              ++i;
              if i < s.length() && ((c = s.charAt)) == '+' || c == '-') )
                ++i;
              break;
            default:
              atEnd = true;
          }
        }
        String num = s.substringstart, end );
        try {
          if state == INT )
            return new Integernum );
          else
            return new Doublenum );
        
        catch NumberFormatException e ) {
          throw new RuntimeException"internal error: " + e );
        }
      }
    }
    throw new NumberFormatException);
  }

  public static String[] toStringsObject[] ) {
    if null == o )
      return null;
    int len = o.length;
    String[] s = new Stringlen ];
    for int i = 0; i < len; i++ )
      if null != o] )
        s= o].toString();
    return s;
  }

  /** converts to int with radix 10 and default 0 */
  public static int toInt Object o ) {
    return toInto, 10);
  }

  /**
   * Convets an Object to an int value using radix 10.
   * <br />This is a wrapper for
   * <code>{@link #toInt( Object, int ) toInt( o, 10 )}</code>.
   @param o Object to be converted to integer
   @return the int value represented by o or 0 if conversion fails.
   */
  public static int toInt Object o, int dflt ) {
    return toInto, 10, dflt );
  }

  /**
   * Converts any String representation to an int value using a given radix.
   * <br />For <code>null</code> it returns <code>0</code>.<br />
   * For Objects instanceof Number it returns the Object's
   * <code>intValue()</code>.
   * For all other Objects it uses the <code>toString()</code> method to get an
   * appropriate String representation and parses this String using
   * <code>Integer.parseInt</code>.
   * If conversion fails it returns <code>0</code>.
   @param o Object to be converted to integer
   @param radix the radix used for conversion
   @return the int value represented by <code>o</code> or <code>0</code>
   * if conversion fails.
   */
  public static int toInt Object o, int radix, int dflt ) {
    if null == o // shortcut without exception
      return dflt;
    if instanceof Number )
      return ((Numbero).intValue();
    try {
      return Integer.parseInto.toString().trim(), radix );
    }
    catch Exception e ) {
      return dflt;
    }
  }

  public static long toLong Object o ) {
    return toLongo, 10);
  }

  public static long toLong Object o, int dflt ) {
    return toLongo, 10, dflt );
  }

  public static long toLong Object o, int radix, int dflt ) {
    if null == o // shortcut without exception
      return dflt;
    if instanceof Number )
      return ((Numbero).longValue();
    try {
      return Long.parseLongo.toString().trim(), radix );
    }
    catch Exception e ) {
      return dflt;
    }
  }

// class Convert

   
    
  
Related examples in the same category
1. Cast a float or double to an integral valueCast a float or double to an integral value
2. A simple casting demo
3. Casting Demo
4. Get the minimum and maximum value of a primitive data types?
5. Class with methods for type conversion
6. Data type conversion
7. Data type Conversion Util
8. Convert Byte array to Int
9. Convert Number To Target Class
10. Convert byte array to Long
11. Convert long to Bytes
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.