Java String to Byte Array asBytes(String basicString)

Here you can find the source of asBytes(String basicString)

Description

Get the corresponding byte array for a basic string.

License

Open Source License

Parameter

Parameter Description
basicString the basic PDF string, as offered by PDFObject#getStringValue()

Return

the bytes corresponding to its characters

Declaration

public static byte[] asBytes(String basicString) 

Method Source Code

//package com.java2s;
/*/*from   www.  ja v a  2  s .  c o m*/
 * Copyright 2008 Pirion Systems Pty Ltd, 139 Warry St,
 * Fortitude Valley, Queensland, Australia
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

public class Main {
    /**
     * Get the corresponding byte array for a basic string. This is effectively
     * the char[] array cast to bytes[], as chars in basic strings only use the
     * least significant byte.
     *
     * @param basicString the basic PDF string, as offered by {@link
     *  PDFObject#getStringValue()}
     * @return the bytes corresponding to its characters
     */
    public static byte[] asBytes(String basicString) {
        final byte[] b = new byte[basicString.length()];
        for (int i = 0; i < b.length; ++i) {
            b[i] = (byte) basicString.charAt(i);
        }
        return b;
    }
}

Related

  1. asBytes(String hex)
  2. asBytes(String hexStr)
  3. convertStringToByte(String input)
  4. convertStringToByte(String strValue)