Java Byte Array from getBytesInUsAscii(String s)

Here you can find the source of getBytesInUsAscii(String s)

Description

Returns the bytes of the string in ENCODING_US_ASCII.

License

Open Source License

Parameter

Parameter Description
s a parameter

Return

a byte[] representation of the string in US-ASCII encoding.

Declaration

public static byte[] getBytesInUsAscii(String s) 

Method Source Code


//package com.java2s;
/*// w w w. j  a  v  a2s.c  o m
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2007 Phex Development Group
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  --- SVN Information ---
 *  $Id: StringUtils.java 4284 2008-10-25 14:24:14Z gregork $
 */

import java.io.UnsupportedEncodingException;

public class Main {
    public static final String ENCODING_ISO_8859_1 = "ISO-8859-1";
    public static final String ENCODING_US_ASCII = "US-ASCII";

    /**
     * Returns the bytes of the string in ENCODING_US_ASCII.
     * In case a exception is raised standard s.getBytes() is called.
     *
     * @param s
     * @return a byte[] representation of the string in US-ASCII encoding.
     */
    public static byte[] getBytesInUsAscii(String s) {
        try {
            return s.getBytes(ENCODING_US_ASCII);
        } catch (UnsupportedEncodingException e) {
            return s.getBytes();
        }
    }

    /**
     * Returns the bytes of the string in ENCODING_ISO_8859_1.
     * In case a exception is raised standard s.getBytes() is called.
     *
     * @param s
     * @return a byte[] representation of the string in ISO_8859_1 encoding.
     */
    public static byte[] getBytes(String s) {
        try {
            return s.getBytes(ENCODING_ISO_8859_1);
        } catch (UnsupportedEncodingException e) {
            return s.getBytes();
        }
    }
}

Related

  1. getBytesFromResource(String resource)
  2. getBytesFromStream(int length, ByteArrayInputStream bais)
  3. getBytesFromString(final String str, final int length, final String coding)
  4. getBytesFromText(String text, String charset)
  5. getBytesInCodePage(final String string, final int codepage)
  6. getBytesISO88591(String s)
  7. getBytesLengthOfEncoding(String encoding, String str)
  8. getByteString(RandomAccessFile inputStream, int numBytesToRead)