Java Byte Array to String by Charset convertToBytes(final String str, final Charset charset)

Here you can find the source of convertToBytes(final String str, final Charset charset)

Description

convert To Bytes

License

Open Source License

Declaration

public static byte[] convertToBytes(final String str, final Charset charset) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class Main {
    public static byte[] convertToBytes(final String str, final Charset charset) {
        return str.getBytes(charset);
    }/* w w  w  . ja  v  a2s .c  o  m*/

    public static byte[] convertToBytes(final String str, final String charsetName) {
        return convertToBytes(str, Charset.forName(charsetName));
    }

    public static byte[] convertToBytes(final String str) {
        return convertToBytes(str, Charset.defaultCharset());
    }

    public static byte[] convertToBytes(final char[] chars, final Charset charset) {
        CharBuffer cb = CharBuffer.allocate(chars.length);
        cb.put(chars);
        cb.flip();
        ByteBuffer bb = charset.encode(cb);
        return bb.array();
    }

    public static byte[] convertToBytes(final char[] chars, final String charsetName) {
        return convertToBytes(chars, Charset.forName(charsetName));
    }

    public static byte[] convertToBytes(final char[] chars) {
        return convertToBytes(chars, Charset.defaultCharset());
    }

    public static byte[] convertToBytes(final short s) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (s & 0xff);
        bytes[1] = (byte) ((s & 0xff00) >> 8);
        return bytes;
    }

    public static byte[] convertToBytes(final char c) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (c);
        bytes[1] = (byte) (c >> 8);
        return bytes;
    }

    public static byte[] convertToBytes(final int i) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (i & 0xff);
        bytes[1] = (byte) ((i & 0xff00) >> 8);
        bytes[2] = (byte) ((i & 0xff0000) >> 16);
        bytes[3] = (byte) ((i & 0xff000000) >> 24);
        return bytes;
    }

    public static byte[] convertToBytes(final long l) {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) (l & 0xff);
        bytes[1] = (byte) ((l >> 8) & 0xff);
        bytes[2] = (byte) ((l >> 16) & 0xff);
        bytes[3] = (byte) ((l >> 24) & 0xff);
        bytes[4] = (byte) ((l >> 32) & 0xff);
        bytes[5] = (byte) ((l >> 40) & 0xff);
        bytes[6] = (byte) ((l >> 48) & 0xff);
        bytes[7] = (byte) ((l >> 56) & 0xff);
        return bytes;
    }

    public static byte[] convertToBytes(final float f) {
        int intBits = Float.floatToIntBits(f);
        return convertToBytes(intBits);
    }

    public static byte[] convertToBytes(final double d) {
        long intBits = Double.doubleToLongBits(d);
        return convertToBytes(intBits);
    }
}

Related

  1. byteToString(Charset charset, byte[] data)
  2. changeEncoding(byte[] byteArray, Charset charsetFrom, Charset charsetTo)
  3. charSequence2Bytes(CharSequence sb, Charset charset)
  4. charSetForBOM(byte[] buffer, int offset)
  5. convertCharset(byte[] content, Charset fromCharset, Charset toCharset)
  6. convertToChars(final byte[] bytes, final Charset charset)
  7. convertToString(byte[] bytes, Charset charset)
  8. convertToString(final byte[] bytes, final Charset charset)
  9. decode(CharsetDecoder decoder, byte[] in)