Java Byte Array Create toBytes(char value)

Here you can find the source of toBytes(char value)

Description

to Bytes

License

Apache License

Declaration

public static byte[] toBytes(char value) 

Method Source Code

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

public class Main {
    public static byte[] toBytes(char value) {
        byte[] bt = new byte[2];
        for (int i = 0; i < bt.length; i++) {
            bt[i] = (byte) (value >>> i * 8);
        }//from w  w  w.j ava2 s  .  com
        return bt;
    }

    public static byte[] toBytes(short value) {
        byte[] bt = new byte[2];
        for (int i = 0; i < bt.length; i++) {
            bt[i] = (byte) (value >>> i * 8);
        }
        return bt;
    }

    public static byte[] toBytes(int value) {
        byte[] bt = new byte[4];
        for (int i = 0; i < bt.length; i++) {
            bt[i] = (byte) (value >>> i * 8);
        }
        return bt;
    }

    public static byte[] toBytes(long value) {
        byte[] bt = new byte[8];
        for (int i = 0; i < bt.length; i++) {
            bt[i] = (byte) (int) (value >>> i * 8);
        }
        return bt;
    }
}

Related

  1. toBytes(byte[] b, int offset, long val)
  2. toBytes(byte[] buffer, int pos, String string)
  3. toBytes(byte[] ipAddress, int port)
  4. toBytes(Byte[] values)
  5. toBytes(char c)
  6. toBytes(char[] cbuf, byte[] bs)
  7. toBytes(char[] chars)
  8. toBytes(final byte[] s, final int off, final int len)
  9. toBytes(final long n)