Byte.valueOf() radix in Java

Byte.valueOf(): This method is available in the java.lang.Byte class of Java.

Syntax:

Byte java.lang.Byte.valueOf(String s, int radix) throws NumberFormatException.

This method takes two arguments, one of type String and another of type int. This method returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed byte in the radix specified by the second argument.

Parameters: Two parameters are required for this method.

s: the string to be parsed radix the.

radix: to be used in interpreting s.

Returns: a Byte object holding the value represented by the string argument in the specified radix.

Throws: NumberFormatException - If the String does not contain a parsable byte.

For Example:

Byte.valueOf("101001",2) = > It returns 41. 

Approach

Java

public class BytevalueOfRadix {
    public static void main(String[] args) {

        String s = "101001";
        int radix = 2;
        System.out.println(Byte.valueOf(s, radix));
    }
}

Output:

41

No comments:

Post a Comment