int a = 0xFF004354;
System.out.println(a);
E quando executo da como resultado o valor: -16759980
porem quanto tento usar o seguinte codigo
String s = "0xFF004354";
int b = Integer.valueOf(s);
System.out.println(b);
Obtenho o seguinte erro:
Exception in thread “main” java.lang.NumberFormatException: For input string: “0xFF004354”
Como posso converter a string “0xFF004354” em Inteiro
Carinha, olha sempre a referência da API: ó, to colando da API reference.
valueOf
public static Integer valueOf(String s)
throws NumberFormatException
Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) 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))
Parameters:
s - the string to be parsed.
Returns:
an Integer object holding the value represented by the string argument.
Throws:
NumberFormatException - if the string cannot be parsed as an integer.
Se você quiser converter números hexadecimais, você tem que usar outro método:
valueOf
public static Integer valueOf(String s,
int radix)
throws NumberFormatException
Returns 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.
Agora, tem um porém na história: ele não aceita o “x” que você está colocando na String. Ele só aceita dígitos (ou letras se a base for maior que 10) . De novo, carinha, olhe a API.
Eu li a API do String.valueOf(), mesmo usando o valor de radix para 16 ele não aceita o valor 0xFF616261 nem FF616261. Parece que ele so aceita ate seis caracteres, por exemplo 616261.
Veja o codigo a seguir
int a = 0xFF004354;
Ele gera um int e no caso um valor negativo, é esse valor que eu preciso.
Porém se eu usar o codigo assim
int a = FF004354; da erro, pois isso resulta em um long
long a = FF004354; Isso ate funciona, mas da um valor diferente do que eu quero.
To completamente perdido viu…
Carinha, isso é porque o java olha o valor e entende que é um long. Mas se você der um cast para int, vai ficar negativo, do jeito que você quer.
lpublic class teste {
public static void main(String[] args) throws ParseException {
long l = Long.parseLong("FFFFFFFF",16);
int i = (int) l;
System.out.println(i);
}
}
Lembre-se que em Java um número inteiro é sempre representado com sinal. Portanto, o maior número possível é 7FFFFFFF (primeiro bit zerado).
Como o número que você quer é a partir de 0xF0000000 (próximo na sequência após 7FFFFFFF), você não poderá usar um int para representá-lo. Por isso, deve usar o método parseLong que o colega indicou. O cast não altera o valor dos bits (a menos que o número tenha bits a mais), por isso, o primeiro bit de 0xFF004354 é interpretado como bit de sinal e o número se torna negativo.