[RESOLVIDO]String para Hexadecimal

8 respostas
fabricioempresa

Bom Pessoal gostarioa de saber se alguem poderia me ajudar converter um string em hexadecimal

segue o código para ilustrar a minha dúvida.

public void Calibrate() throws ParseException {
           StringBuffer result = new StringBuffer();
       for (int j=1;j<=8;j++){
        if (FileConfig.getInstance().getAtivar(j).equals("Ativo")){
           result.append(1);
        }
        else{
           result.append(0);
        }
        JOptionPane.showMessageDialog(result.toString());

       }

Gostaria de transformar esse result.toString em hexadecimal.

Desde já agradeço a todos que me ajudarem.

8 Respostas

marcelo.bellissimo

String hexa = Integer.toHexString(15); System.out.println(hexa);//isso imprime 'f'

fabricioempresa

Não testei ainda mas tu sabe se tem como deixar isso em hexa msm?

Essa minha String retorna o valor em binario.

lina

Oi,

Se for fazer na mão, poderá ser feito algo do tipo:

public static String ByteToHexa(final byte ah_to_convert)
	{

		// Conterá o resultado.
		byte[] lh_result = new byte[2];

		//
		// low-order semi-bytes ...
		//
		lh_result[0] = (byte) (ah_to_convert >> 4 & 0x0F);
		lh_result[1] = (byte) (ah_to_convert & 0x0F);

		// high-order value ...
		lh_result[0] += (lh_result[0] < 10 ? 0x30 : 0x37);
		lh_result[1] += (lh_result[1] < 10 ? 0x30 : 0x37);

		// Converte um array de bytes em String e o retorna.
		return (new String(lh_result));
	}

	public static void main(String[] args)
	{
		// String a ser "transformada".
		byte[] ah_to_convert = "TESTANDO".getBytes();

		// Resultado.
		StringBuilder lo_result = new StringBuilder(
				ah_to_convert.length * 2);

		int an_start = 0;

		// Converte o range para hexa.
		for (; an_start < ah_to_convert.length; lo_result.append("")
				.append(ByteToHexa(ah_to_convert[an_start++])))
			;

		// formata o resultado.
		String ls_result = lo_result.substring("".length());

		System.out.println(ls_result);
	}

Tchauzin!

walissongpi

tente:

Integer.toHexString(Integer.parseInt(result.toString()));
marcelo.bellissimo

fabricioempresa:
Não testei ainda mas tu sabe se tem como deixar isso em hexa msm?

Essa minha String retorna o valor em binario.

Como assim? Em hexa é isso:
0=0
1=1

10=a
11=b
12=c
13=d
14=e
15=f

Se o seu 1 é binário, então primeiro você converte pra um número decimal, depois pra hexa, assim:

int binario= Integer.parseInt("0101",2); String hexa = Integer.toHexString(binario); System.out.println(hexa);

marcelo.bellissimo

[quote=walissongpi]tente:

Integer.toHexString(Integer.parseInt(result.toString()));

Precisa passar a base, dessa maneira você só arredonda o número… o número 1000 em binário representa um 8, desse jeito você transformaria o 1000 ao invés do 8 para hexadecimal…

lina
fabricioempresa:
Bom Pessoal gostarioa de saber se alguem poderia me ajudar converter um string em hexadecimal

segue o código para ilustrar a minha dúvida.

public void Calibrate() throws ParseException {
           StringBuffer result = new StringBuffer();
       for (int j=1;j<=8;j++){
        if (FileConfig.getInstance().getAtivar(j).equals("Ativo")){
           result.append(1);
        }
        else{
           result.append(0);
        }
        JOptionPane.showMessageDialog(result.toString());

       }

Gostaria de transformar esse result.toString em hexadecimal.

Desde já agradeço a todos que me ajudarem.

Oi,

Deu certo?!

Tchauzin!

fabricioempresa
public static int Conversion() {
        int j, hexa = 0, hex = 0;
        for (j = 1; j <= 8; j++) {
            if (FileConfig.getInstance().getAtivar(j).equals("Ativo")) {
                hex = 1;
            } else {
                hex = 0;
            }
            hexa += (int) (hex * (Math.pow(2, j - 1)));

        }
        return hexa;
    }

Bom pessoal acredito que pensei um pouco mais e resolvi xD

Transformei em hexadecimal desse jeito e me resolveu o problema

Criado 18 de maio de 2010
Ultima resposta 18 de mai. de 2010
Respostas 8
Participantes 4