Java Program to Convert String to Byte

You are currently viewing Java Program to Convert String to Byte

Java program to convert string to byte

// Java program to convert 
// string to byte

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner X = new Scanner(System.in);
    String str;

    System.out.print("Enter string: ");
    str = X.next();

    byte byteValue = 0;

    //Using byteValue() method.
    byteValue = Byte.valueOf(str).byteValue();
    System.out.println("Byte value: " + byteValue);

    //Using parseByte() method.
    byteValue = Byte.parseByte(str);
    System.out.println("Byte value: " + byteValue);
  }
}

Output

Enter string: 123
Byte value: 123
Byte value: 123

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply