Java Char Convert To charToBasicType(char c, Class clazz)

Here you can find the source of charToBasicType(char c, Class clazz)

Description

char To Basic Type

License

LGPL

Declaration

@SuppressWarnings("unchecked")
    public static <T> T charToBasicType(char c, Class<T> clazz) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T charToBasicType(char c, Class<T> clazz) {
        if (clazz.equals(Boolean.class))
            return (T) Boolean.valueOf(charToBoolean(c));
        if (clazz.equals(Byte.class))
            return (T) charToByte(c);
        if (clazz.equals(Character.class))
            return (T) Character.valueOf(c);
        if (clazz.equals(Short.class))
            return (T) Short.valueOf((short) c);
        if (clazz.equals(Integer.class))
            return (T) Integer.valueOf(c);
        if (clazz.equals(Long.class))
            return (T) Long.valueOf(c);
        if (clazz.equals(Float.class))
            return (T) Float.valueOf(c);
        if (clazz.equals(Double.class))
            return (T) Double.valueOf(c);
        if (clazz.equals(String.class))
            return (T) String.valueOf(c);

        return null;
    }/*from  ww  w  .  j a  v  a  2  s  .  c o  m*/

    public static boolean charToBoolean(char c) {
        return c != 0 ? true : false;
    }

    public static Byte charToByte(char c) {
        if (c >= Byte.MIN_VALUE && c <= Byte.MAX_VALUE) {
            return (byte) c;
        }
        return null;
    }

    public static byte charToByte(char c, byte defaultValue) {
        if (c >= Byte.MIN_VALUE && c <= Byte.MAX_VALUE) {
            return (byte) c;
        }
        return defaultValue;
    }
}

Related

  1. charTo2Byte(char x)
  2. CharToASCII(final char character)
  3. charToBoolean(char c)
  4. charToBuffer(char ch, byte[] ioBuffer)
  5. charToCDATA(char ch)
  6. charToCompressedBaseLow(final int base)