Conversão de c odigos PHP para Java

7 respostas
J

Hello people!

Alguem ai entende php para tranformar este metodo em java?

Eu ja tentei, mas não entendo nada de PHP.

function convert($src, $srcAlphabet, $dstAlphabet) {
    $srcBase = strlen($srcAlphabet);
    $dstBase = strlen($dstAlphabet);

    $wet = $src;
    $val = 0;
    $mlt = 1;

    while ($l = strlen($wet)) {
        $digit = $wet[$l - 1];
        $val += $mlt * strpos($srcAlphabet, $digit);
        $wet = substr($wet, 0, $l - 1);
        $mlt *= $srcBase;
    }

    $wet = $val;
    $dst = '';

    while ($wet >= $dstBase) {
        $digitVal = $wet % $dstBase;
        $digit = $dstAlphabet[$digitVal];
        $dst = $digit . $dst;
        $wet /= $dstBase;
    }

    $digit = $dstAlphabet[$wet];
    $dst = $digit . $dst;

    return $dst;
}

// prints cb
print convert('125', '[telefone removido]', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');

// prints 19158
print convert('e9a', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', '[telefone removido]');

Obrigado a quem puder me ajudar

7 Respostas

felipebonezi

Esse código faz oque?

romarcio

Se não estou enganado

strlen($wet) retorna o tamanho, tipo var.lenght() no java.
strpos($srcAlphabet, $digit); seria o método replace() ou substring()

Acho que de resto não tem muito mistério.

J

Este código converte é para encurtar url, como abaixo:

jamirdeajr

strpos($srcAlphabet, $digit);
retorna em que posição digit existe em srcAlphabet, em java seria equivalente ao indexOf

jamirdeajr
Vai ai a conversão de PHP para Java. Não me preocupei em otimizar nada, apenas fiz funcionar ok!
public static String convert(String src, String srcAlphabet, String dstAlphabet) {
		
	    int srcBase = srcAlphabet.length();
	    int dstBase = dstAlphabet.length();
		
	    String wet = src;
	    String digit;
	    String dst;
	    int val = 0;
	    int mlt = 1;
	    int l = wet.length();
	    
	    while (l != 0) {
	        digit = wet.substring(l - 1,l);
	        val += mlt * srcAlphabet.indexOf(digit);
	        wet = wet.substring(0,l-1);
	        mlt *= srcBase;
	        
	        l = wet.length();
	    }

	    int wetInt = val;
	    dst = "";
	    int digitVal;

	    while (wetInt >= dstBase) {
	        digitVal = wetInt % dstBase;
	        digit = dstAlphabet.substring(digitVal,digitVal+1);
	        dst = digit + dst;
	        wetInt /= dstBase;
	    }
	    digit = dstAlphabet.substring(wetInt,wetInt+1);
	    dst = digit + dst;
	    return dst;
   
	}
	public static void main(String[] args) {
		
		// prints cb
		System.out.println(convert("125", "[telefone removido]", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));

		// prints 19158
		System.out.println(convert("e9a", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "[telefone removido]"));

	}
J
jamirdeajr:
Vai ai a conversão de PHP para Java. Não me preocupei em otimizar nada, apenas fiz funcionar ok!
public static String convert(String src, String srcAlphabet, String dstAlphabet) {
		
	    int srcBase = srcAlphabet.length();
	    int dstBase = dstAlphabet.length();
		
	    String wet = src;
	    String digit;
	    String dst;
	    int val = 0;
	    int mlt = 1;
	    int l = wet.length();
	    
	    while (l != 0) {
	        digit = wet.substring(l - 1,l);
	        val += mlt * srcAlphabet.indexOf(digit);
	        wet = wet.substring(0,l-1);
	        mlt *= srcBase;
	        
	        l = wet.length();
	    }

	    int wetInt = val;
	    dst = "";
	    int digitVal;

	    while (wetInt >= dstBase) {
	        digitVal = wetInt % dstBase;
	        digit = dstAlphabet.substring(digitVal,digitVal+1);
	        dst = digit + dst;
	        wetInt /= dstBase;
	    }
	    digit = dstAlphabet.substring(wetInt,wetInt+1);
	    dst = digit + dst;
	    return dst;
   
	}
	public static void main(String[] args) {
		
		// prints cb
		System.out.println(convert("125", "[telefone removido]", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));

		// prints 19158
		System.out.println(convert("e9a", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "[telefone removido]"));

	}

Muito Obrigado jamirdeajr!
Você fez um ótimo trabalho, eu quebrei a cabeça tentando resolver isso, fiquei um dia inteiro tentando mas não entendo PHP.

Te agradeço de verdade, e gostaria até de pagar uma cerveja :) :D :D

Grande abraço!

jamirdeajr

Não precisa não, foi até divertido de fazer!
Abraço!

Criado 26 de março de 2011
Ultima resposta 27 de mar. de 2011
Respostas 7
Participantes 4