English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java程序将字符串转换为字节

使用该valueOf()方法将字符串转换为字节。首先,让我们来一串。

String str = "65";

现在,使用该valueOf()方法将字符串转换为字节。

byte res = Byte.valueOf(str);

让我们看完整的例子。

示例

public class Demo {
   public static void main(String[] args) {
      String str = "65";
      System.out.println("String: "+str);
      byte res = Byte.valueOf(str);
      System.out.println("Byte: "+res);
   }
}

输出结果

String: 65
Byte: 65