Java Byte Array Create toBytes(final String text)

Here you can find the source of toBytes(final String text)

Description

Convert String into byte array

License

Open Source License

Parameter

Parameter Description
text String

Return

byte array, always including '\0' termination

Declaration

final public static byte[] toBytes(final String text) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/

public class Main {
    /** Convert String into byte array
     *  @param text {@link String}// w  w w.  j  a  va2s.co m
     *  @return byte array, always including '\0' termination
     */
    final public static byte[] toBytes(final String text) { // Write string as byte array WITH '\0' TERMINATION!
        final byte[] bytes = new byte[text.length() + 1];
        System.arraycopy(text.getBytes(), 0, bytes, 0, text.length());
        bytes[text.length()] = '\0';
        return bytes;
    }
}

Related

  1. toBytes(final byte[] s, final int off, final int len)
  2. toBytes(final long n)
  3. toBytes(final long val)
  4. toBytes(final String hexaString)
  5. toBytes(final String str)
  6. toBytes(int data)
  7. toBytes(int data)
  8. toBytes(int i)
  9. toBytes(int i)