Convert char To Byte - Android java.lang

Android examples for java.lang:Character

Description

Convert char To Byte

Demo Code


//package com.java2s;

public class Main {
    public static byte[] charToByte(char c) {
        byte[] b = new byte[2];
        b[0] = (byte) ((c & 0xFF00) >> 8);
        b[1] = (byte) (c & 0xFF);
        return b;
    }//from  w w w . j  a  v a  2 s. c  om

    public static String toString(byte[] bytes, int size) {
        String ss = "";
        for (int i = 0; i < size; i++) {
            ss += toHex(bytes[i]);
            //+" ";
        }
        return ss;
    }

    public static String toHex(byte b) {
        return ("" + "0123456789ABCDEF".charAt(0xf & b >> 4) + "0123456789ABCDEF"
                .charAt(b & 0xf));
    }
}

Related Tutorials