Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Le radix c'est quoi ?

4 réponses
Avatar
artintel
La methode "parseInt " permet de recuperer un int a partir d'un String
mais comment marche valueOf ? Je ne vois pas ce qu'est un radix. Ca
apparait souvent dans les methodes.


valueOf
public static Integer valueOf(String s, int radix)
throws NumberFormatExceptionReturns an Integer 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 integer in the radix specified by the second
argument, exactly as if the arguments were given to the parseInt
(java.lang.String, int) method. The result is an Integer object that
represents the integer value specified by the string. In other words,
this method returns an Integer object equal to the value of:

new Integer(Integer.parseInt(s, radix))

Parameters:
s - the string to be parsed.
radix - the radix to be used in interpreting s
Returns:
an Integer object holding the value represented by the string argument
in the specified radix.
Throws:
NumberFormatException - if the String does not contain a parsable int.

4 réponses

Avatar
Albert
artintel a écrit :
La methode "parseInt " permet de recuperer un int a partir d'un String
mais comment marche valueOf ? Je ne vois pas ce qu'est un radix. Ca
apparait souvent dans les methodes.



c'est la "base" au sens mathématique. Pour les nombres décimaux (les
plus courants), c'est la base 10, mais il y a aussi la base 2 et la base
16 qui sont connues en informatique.
Avatar
Alain Ketterlin
artintel writes:

La methode "parseInt " permet de recuperer un int a partir d'un String
mais comment marche valueOf ?



parseInt renvoie un int, alors que valueOf renvoie une instance de
Integer (éventuellement "cachée").

Je ne vois pas ce qu'est un radix. Ca apparait souvent dans les
methodes.



C'est la base de numération.

-- Alain.
Avatar
JavaBeaucoupMieux
"artintel" a écrit dans le message de news:

Parameters:
s - the string to be parsed.
radix - the radix to be used in interpreting s



class PourTests {

public static void main(String[] args) {
String nombre;
nombre="11001011";

int base10,base2,defaut;

base10=Integer.valueOf(nombre, 10);
base2=Integer.valueOf(nombre, 2);
defaut=Integer.valueOf(nombre);

System.out.println("En base 10 : "+base10);
System.out.println("En base 2 : "+base2);
System.out.println("Par défaut : "+defaut);

}
}


--------------------------------------------------
En base 10 : 11001011
En base 2 : 203
Par défaut : 11001011
--------------------------------------------------

:-)
Avatar
artintel
On 2 sep, 22:39, "JavaBeaucoupMieux" <@> wrote:
"artintel" a écrit dans le message de news:

> Parameters:
> s - the string to be parsed.
> radix - the radix to be used in interpreting s

class PourTests {

     public static void main(String[] args) {
     String nombre;
     nombre="11001011";

     int base10,base2,defaut;

     base10=Integer.valueOf(nombre, 10);
     base2=Integer.valueOf(nombre, 2);
     defaut=Integer.valueOf(nombre);

     System.out.println("En base 10 : "+base10);
     System.out.println("En base  2 : "+base2);
     System.out.println("Par défaut : "+defaut);

    }

}

--------------------------------------------------
En base 10 : 11001011
En base  2 : 203
Par défaut : 11001011
--------------------------------------------------

:-)



Merci a tous pour vos reponses ;o)