Get the corresponding byte array for a basic string. - Java java.lang

Java examples for java.lang:byte Array to String

Description

Get the corresponding byte array for a basic string.

Demo Code

//package com.java2s;

public class Main {
    /**/* w  ww.  j  a v a  2 s  .  c o  m*/
     * 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 Tutorials