Java String to Byte Array getBytes(String s, String encoding)

Here you can find the source of getBytes(String s, String encoding)

Description

Returns the contents of the given string as an array of bytes in the platform's default encoding.

License

Open Source License

Declaration

static byte[] getBytes(String s, String encoding) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 * 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
 *
 * Contributors://  w  w  w. j av  a2s  . com
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Returns the contents of the given string as an array of bytes
     * in the platform's default encoding.
     */
    static byte[] getBytes(String s, String encoding) {
        try {
            return s.getBytes(encoding);
        } catch (UnsupportedEncodingException e) {
            return s.getBytes();
        }
    }
}

Related

  1. getBytes(String s)
  2. getBytes(String s)
  3. getBytes(String s)
  4. getBytes(String s, String charsetName)
  5. getBytes(String s, String encoding)
  6. getBytes(String str)
  7. getBytes(String str)
  8. getBytes(String str)
  9. getBytes(String str)