package br.com.actio.util;


import java.util.*;

/**
 * Classe para converção de EBCDIC para ASCII
 * Creation date: (5/7/02 17:46:04)
 * Métodos:
 * _ converte(String str)
 * _ ebcdicToAscii(String ebcdicString)
 * _ posValor(int valor, int[] valores)
 * @author: Administrator
 */
 
public class Converte {

/**
 * Converte uma string EBCDIC para ASCII
 * Creation date: (5/7/02 17:46:04)
 * @return Retorna uma String em ASCII
 * @param str Parâmetro do tipo String em EDCDIC
 */

public static String converte(String str) {
	if (str.length() % 2 != 0)
		return null;

	StringBuffer stb = new StringBuffer();

	for (int i = 0 ; i < str.length() ; i+= 2) {
		stb.append((char) Integer.valueOf(str.substring(i,i+2), 16).intValue());
	}
	
		
	return ebcdicToAscii(stb.toString());
}

 /**
 * Transforma uma String em EBCDIC em ASCII
 * Creation date: (9/5/2002 15:34:03)
 * @return Retorna uma String em ASCII
 * @param ebcdicString Parâmetro do tipo String em EDCDIC
 */
 
private static String ebcdicToAscii(String ebcdicString) {

	int[] ebcdic = {0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 21, 11, 12, 13, 14, 15, 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31, 64, 90, 127, 123, 91, 108, 80, 125, 77, 93, 92, 78, 107, 96, 75, 97, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 122, 94, 76, 126, 110, 111, 124, 193, 194, 195, 196, 197, 198, 199, 200, 201, 209, 210, 211, 212, 213, 214, 215, 216, 217, 226, 227, 228, 229, 230, 231, 232, 233, 173, 224, 189, 95, 109, 121, 129, 130, 131, 132, 133, 134, 135, 136, 137, 145, 146, 147, 148, 149, 150, 151, 152, 153, 162, 163, 164, 165, 166, 167, 168, 169, 192, 79, 208, 161, 7, 32, 33, 34, 35, 36, 37, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27, 48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62, 255, 65, 170, 74, 177, 159, 178, 106, 181, 187, 180, 154, 138, 176, 202, 175, 188, 144, 143, 234, 250, 190, 160, 182, 179, 157, 218, 155, 139, 183, 184, 185, 171, 100, 101, 98, 102, 99, 103, 158, 104, 116, 113, 114, 115, 120, 117, 118, 119, 172, 105, 237, 238, 235, 239, 236, 191, 128, 253, 254, 251, 252, 186, 174, 89, 68, 69, 66, 70, 67, 71, 156, 72, 84, 81, 82, 83, 88, 85, 86, 87, 140, 73, 205, 206, 203, 207, 204, 225, 112, 221, 222, 219, 220, 141, 142, 223};
	String asciiString = "";
	for (int i = 0; i < ebcdicString.length(); i++)
	{
		asciiString += ((char) posValor((int) ebcdicString.charAt(i), ebcdic)) + "";
	}
	return asciiString;

} 

/**
 * Retorna a posição do valor dentro do vetor de valores
 * Creation date: (9/5/2002 15:34:03)
 * @return Retorna int que representa a posição do valor
 * @param valor Parâmetro int que representa o valor a encontrar
 * @param valores Parâmetro int[] que representa vetor de valores
 */
 
private static int posValor(int valor, int[] valores)
{
	for (int i = 0; i < valores.length; i++)
	{
		if (valores[i] == valor)
			return i;
	}
	return -1;
}
}
