java byte 方法_Java Byte decode()方法

Java Byte decode()方法

java.lang.Byte.decode(String nm) 解码字符串转换为字节。接受十进制,十六进制,并通过以下语法给出八进制数:

解码字符串:

Signopt DecimalNumeral

Signopt 0x HexDigits

Signopt 0X HexDigits

Signopt # HexDigits

Signopt 0 OctalDigits

SIGN

+

字符以下的可选符号和/或基数说明符(“0x”,“0X”,“#”,或前导零),该序列被解析为受Byte.parseByte方法与指示的基数(10,16,或8)。

此字符序列必须代表一个正值,否则将抛出NumberFormatException。如果指定的字符串的第一个字符是减号,结果是负数。任何空白字符被允许在字符串。

1 语法

public static Byte decode(String nm)throws NumberFormatException

2 参数

nm:字符串进行解码

3 返回值

此方法将返回一个字节对象持有nm的所代表的字节值。

4 示例

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* Java Byte decode()方法

*/

import java.lang.*;

public class ByteDemo {

public static void main(String[] args) {

// create 4 Byte objects

Byte b1, b2, b3, b4;

/**

* static methods are called using class name.

* decimal value is decoded and assigned to Byte object b1

*/

b1 = Byte.decode("100");

// hexadecimal values are decoded and assigned to Byte objects b2, b3

b2 = Byte.decode("0x6b");

b3 = Byte.decode("-#4c");

// octal value is decoded and assigned to Byte object b4

b4 = Byte.decode("0127");

String str1 = "Byte value of decimal 100 is " + b1;

String str2 = "Byte value of hexadecimal 6b is " + b2;

String str3 = "Byte value of hexadecimal -4c is " + b3;

String str4 = "Byte value of octal 127 is " + b4;

// print b1, b2, b3, b4 values

System.out.println( str1 );

System.out.println( str2 );

System.out.println( str3 );

System.out.println( str4 );

}

}

输出结果为:

Byte value of decimal 100 is 100

Byte value of hexadecimal 6b is 107

Byte value of hexadecimal -4c is -76

Byte value of octal 127 is 87

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值