/* 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */
/** 
	 * HostToIP.java
	 * Autor: Alberto Población Navarro
	 * Añadido al proyecto en la version v0.2.2
	 * Añadido a:
	 		* Proyecto original: http://code.google.com/p/android-vnc-viewer/
	 		* Release: 39
	 */
package android.androidVNC;

import java.net.InetAddress;
import java.net.UnknownHostException;

import android.util.Log;

public class HostToIP {

	private static final String TAG = "HostToIP";
	/**
	 * Convierte el nobmre del host en una dirección IP
	 * @param address
	 * @return
	 */
	public static String convertir(String address) {
		String ipAddrStr="";
		try {
	    	//Obtenemos la direccion por su nombre(escrito en el editbox)
	        InetAddress addr = InetAddress.getByName(address);
	        byte[] ipAddr = addr.getAddress();
	        // Convertimos la dirección al formato xx.xx.xx.xx
	        for (int i=0; i<ipAddr.length; i++) {
	            if (i > 0) {
	                ipAddrStr += ".";
	            }
	         //Al realizar esta operación se convirten los numeros negativos en positivos
	            ipAddrStr += ipAddr[i]&0xFF;
	            //Log.d("DEBUG", "Iteracion: "+i+" "+"Cadena: "+ipAddrStr+"||"+ipAddr[i]+"||"+0xFF);	            
	        }
	     } catch (UnknownHostException e) {
	    	Log.v(TAG, "No se encuentra el host, se utilizará la direccion IP");
	    	ipAddrStr=address;
	    }
		return ipAddrStr;
	}

}
