Java Byte Array Create toBytesDirect(final String singleOctets)

Here you can find the source of toBytesDirect(final String singleOctets)

Description

Returns a byte array representing the given string, truncating each character into a byte directly.

License

Apache License

Exception

Parameter Description
IllegalArgumentException if the input string contains any multi-octet character

Declaration

public static byte[] toBytesDirect(final String singleOctets) 

Method Source Code

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

public class Main {
    /**/*  ww  w . j  a v a 2  s .  com*/
     * Returns a byte array representing the given string,
     * truncating each character into a byte directly.
     * 
     * @throws IllegalArgumentException if the input string contains any multi-octet character
     */
    public static byte[] toBytesDirect(final String singleOctets) {
        final char[] src = singleOctets.toCharArray();
        final byte[] dest = new byte[src.length];

        for (int i = 0; i < dest.length; i++) {
            final char c = src[i];

            if (c > Byte.MAX_VALUE)
                throw new IllegalArgumentException(
                        "Invalid character found at position " + i + " for " + singleOctets);
            dest[i] = (byte) c;
        }
        return dest;
    }
}

Related

  1. toBytes4(int n)
  2. toBytes4HexString(String str, char... separateds)
  3. toBytesBE(long v)
  4. toBytesBigEndian(long value, int sizeInByte)
  5. toBytesBinary(String in)
  6. toBytesFromASCII(final char[] chars)
  7. toBytesFromBase64(String inBase64String)
  8. toBytesFromBin(String binSymbols)
  9. toBytesFromHexStr(String hexStr)