Java Convert via ByteBuffer stringsToBinary(final String strings[])

Here you can find the source of stringsToBinary(final String strings[])

Description

Utility to convert a String to bytes

License

Apache License

Parameter

Parameter Description
string incoming String to convert

Return

a byte array

Declaration

public static byte[] stringsToBinary(final String strings[]) 

Method Source Code


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

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;

import java.util.List;

public class Main {
    public static final Charset GEOWAVE_CHAR_SET = Charset.forName("ISO-8859-1");

    /**//from w w  w  .j a v a  2s .c  om
     * Utility to convert a String to bytes
     * 
     * @param string
     *            incoming String to convert
     * @return a byte array
     */
    public static byte[] stringsToBinary(final String strings[]) {
        int len = 4;
        final List<byte[]> strsBytes = new ArrayList<byte[]>();
        for (final String str : strings) {
            final byte[] strByte = str.getBytes(GEOWAVE_CHAR_SET);
            strsBytes.add(strByte);
            len += (strByte.length + 4);

        }
        final ByteBuffer buf = ByteBuffer.allocate(len);
        buf.putInt(strings.length);
        for (final byte[] str : strsBytes) {
            buf.putInt(str.length);
            buf.put(str);
        }
        return buf.array();
    }
}

Related

  1. longToLE(long lVal)
  2. longToShorts2(long value)
  3. shortToByteArray(short inShort)
  4. shortToBytes(short s)
  5. stringsFromBinary(final byte[] binary)
  6. stringToBytes(final String inString)
  7. stringToBytes(String str)
  8. StringToData(String conv, boolean isRe, int number)
  9. stringToIpAddress(String s)