Saturday, March 7, 2015

How to convert data types in Java

P
Java is a strong programming language. It supports DatatypeConverter class for data type converting. There are functions it is supported:

static String  parseAnySimpleType(String lexicalXSDAnySimpleType)
static byte[]  parseBase64Binary(String lexicalXSDBase64Binary)
static boolean  parseBoolean(String lexicalXSDBoolean)
static byte  parseByte(String lexicalXSDByte)
static Calendar  parseDate(String lexicalXSDDate)
static Calendar  parseDateTime(String lexicalXSDDateTime)
static BigDecimal parseDecimal(String lexicalXSDDecimal)
static double  parseDouble(String lexicalXSDDouble)
static float  parseFloat(String lexicalXSDFloat)
static byte[]  parseHexBinary(String lexicalXSDHexBinary)
static int  parseInt(String lexicalXSDInt)
static BigInteger parseInteger(String lexicalXSDInteger)
static long  parseLong(String lexicalXSDLong)
static QName  parseQName(String lexicalXSDQName, NamespaceContext nsc)
static short  parseShort(String lexicalXSDShort)
static String  parseString(String lexicalXSDString)
static Calendar  parseTime(String lexicalXSDTime)
static long  parseUnsignedInt(String lexicalXSDUnsignedInt)
static int  parseUnsignedShort(String lexicalXSDUnsignedShort)
static String  printAnySimpleType(String val)
static String  printBase64Binary(byte[] val)
static String  printBoolean(boolean val)
static String  printByte(byte val)
static String  printDate(Calendar val)
static String  printDateTime(Calendar val)
static String  printDecimal(BigDecimal val)
static String  printDouble(double val)
static String  printFloat(float val)
static String  printHexBinary(byte[] val)
static String  printInt(int val)
static String  printInteger(BigInteger val)
static String  printLong(long val)
static String  printQName(QName val, NamespaceContext nsc)
static String  printShort(short val)
static String  printString(String val)
static String  printTime(Calendar val)
static String  printUnsignedInt(long val)
static String  printUnsignedShort(int val)
static void  setDatatypeConverter(DatatypeConverterInterface converter)

How to use

I just show you how to use some popular functions

package com.it4shared.convert;

import java.util.Arrays;

import javax.xml.bind.DatatypeConverter;

public class Convert {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String text = "it-4shared.com";
  byte[] text_byte = text.getBytes();
  String encodedText = DatatypeConverter.printBase64Binary(text_byte);
  System.out.println("Base64 encode: "+encodedText);
  
  byte[] decodedText = DatatypeConverter.parseBase64Binary(encodedText);
  System.out.println("Base64 decode: "+new String(decodedText));
  
  String hexText = DatatypeConverter.printHexBinary(text_byte);
  System.out.println("HexString: "+hexText);
  
  byte[] parseHexString = DatatypeConverter.parseHexBinary(hexText);
  System.out.println("Compare 2 byte arrays: "+Arrays.equals(text_byte, parseHexString));
 }

Good luck!

Reference

No comments:

Post a Comment