Languages
[Edit]
EN

Java - convert String to byte

12 points
Created by:
Root-ssh
68500

In Java, we can convert String to byte in couple of different ways.

Short solutions:

// solution 1
byte num1 = Byte.parseByte("31");

// solution 2
Byte num2 = Byte.valueOf("31");

// solution 3
Byte num3 = new Byte("31");

1. Using Byte.parseByte()

public class Example1 {

    public static void main(String[] args) {
        String str = "31";
        byte num = Byte.parseByte(str);
        System.out.println(num); // 31
    }
}

Output:

31

2. Using Byte.valueOf()

public class Example2 {

    public static void main(String[] args) {
        String str = "31";
        Byte num = Byte.valueOf(str);
        System.out.println(num); // 31
    }
}

Output:

31

3. Using constructor - new Byte(String s)

public class Example3 {

    public static void main(String[] args) {
        String str = "31";
        Byte num = new Byte(str);
        System.out.println(num); // 31
    }
}

Output:

31

 

Java - String conversion

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join