Java String getBytes()方法与示例

字符串getBytes()方法 (String getBytes() Method)

getBytes() method is a String method in java and it is used to get the byte values of a given string.

getBytes()方法是Java中的String方法,用于获取给定字符串的字节值。

Syntax:

句法:

    byte[] String.getBytes();

It accepts String and returns byte array.

它接受String并返回字节数组。

Example:

例:

    Input: "IncludeHelp"

    Output:
    73
    110
    99
    108
    117
    100
    101
    72
    101
    108
    112

Code:

码:

public class Main
{
    public static void main(String[] args) {
        try{
            
            String str = "IncludeHelp";
            
            byte[] arr = str.getBytes();
            
            //printing byte array (each elements in nubers)
            System.out.println("Byte values of each character...");
            for(int i =0; i<str.length(); i++)
                System.out.println("arr[" + i +"] = " + arr[i]);
        }
        catch(Exception ex){
            System.out.println("Error: " + ex.toString());
        }
    }
}

Output

输出量

Byte values of each character...
arr[0] = 73
arr[1] = 110
arr[2] = 99
arr[3] = 108
arr[4] = 117
arr[5] = 100
arr[6] = 101
arr[7] = 72
arr[8] = 101
arr[9] = 108
arr[10] = 112


翻译自: https://www.includehelp.com/java/string-getBytes-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值