Java. lang. String. getBytes (String charsetName) Method Instance, string. getbytes

Source: Internet
Author: User

Java. lang. String. getBytes (String charsetName) Method Instance, string. getbytes

Java. lang. String. getBytes (String charsetName)The encoding method uses the byte sequence of the specified character set and stores the result to a new byte array.

Statement

Below isJava. lang. String. getBytes ()Method Declaration

public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
Parameters
  • Charset -- this is the name of a supported character set.

Return Value

This method returns the byte array.

Exception
  • UnsupportedEncodingException -- if the specified character set is not supported.

Example

The following example shows how to use java. lang. String. getBytes.

package com.yiibai;import java.lang.*;public class StringDemo {  public static void main(String[] args) throws Exception {      // string with numbers and some special characters        String str = "!$0123@";        // byte array with charset    byte bval[] = str.getBytes("UTF8");      // prints the byte array    for (int i = 0; i < bval.length; i++) {       System.out.println(bval[i]);    }  }}

Let's compile and run the above program, which will produce the following results:

33364849505164

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.