Return to Snippet

Revision: 61738
at January 27, 2013 22:30 by eiger824


Updated Code
package com.santi.encodingtoascii;

import java.io.*;

public class Main {

	public static void main(String[] args) {
		
		String text = getInput("Enter the text to convert to binary code: ");
		System.out.println(text);
		String finalArray="";
		StringBuilder sb = new StringBuilder(finalArray);
		// voy a hacer un bucle que vaya elemento a elemento del array de caracteres
//		y que me lo codifique mediante ascii a binario,
//		luego cada carácter codificado lo meto en un array de números binarios
//		y lo saco por pantalla.
		int len = text.length();
		System.out.println("Length of the text (characters): " +len);
		char [] char_text = text.toCharArray();
		for (int i=0; i<len; i++) {
			char c = char_text[i];
			switch (c) {
			case '0':
				sb.append("00110000");
				break;
			case '1':
				sb.append("00110001");
				break;
			case '2':
				sb.append("00110010");
				break;
			case '3':
				sb.append("00110011");
				break;
			case '4':
				sb.append("00110100");
				break;
			case '5':
				sb.append("00110101");
				break;
			case '6':
				sb.append("00110110");
				break;
			case '7':
				sb.append("00110111");
				break;
			case '8':
				sb.append("00111000");
				break;
			case '9':
				sb.append("00111001");
				break;
			case 'a':
				sb.append("01100001");
				break;
			case 'b':
				sb.append("01100010");
				break;
			case 'c':
				sb.append("01100011");
				break;
			case 'd':
				sb.append("01100100");
				break;
			case 'e':
				sb.append("01100101");
				break;
			case 'f':
				sb.append("01100110");
				break;
			case 'g':
				sb.append("01100111");
				break;
			case 'h':
				sb.append("01101000");
				break;
			case 'i':
				sb.append("01101001");
				break;
			case 'j':
				sb.append("01001010");
				break;
			case 'k':
				sb.append("01101011");
				break;
			case 'l':
				sb.append("01101100");
				break;
			case 'm':
				sb.append("01101101");
				break;
			case 'n':
				sb.append("01101110");
				break;
			case 'o':
				sb.append("01101111");
				break;
			case 'p':
				sb.append("01110000");
				break;
			case 'q':
				sb.append("01110001");
				break;
			case 'r':
				sb.append("01110010");
				break;
			case 's':
				sb.append("01110011");
				break;
			case 't':
				sb.append("01110100");
				break;
			case 'u':
				sb.append("01110101");
				break;
			case 'v':
				sb.append("01110110");
				break;
			case 'w':
				sb.append("01110111");
				break;
			case 'x':
				sb.append("01111000"); 
				break;
			case 'y':
				sb.append("01111001");
				break;
			case 'z':
				sb.append("01111010");
				break;
			case 'A':
				sb.append("01000001");
				break;
			case 'B':
				sb.append("01000010");
				break;
			case 'C':
				sb.append("01000011");
				break;
			case 'D':
				sb.append("01000100");
				break;
			case 'E':
				sb.append("01000101");
				break;
			case 'F':
				sb.append("01000110");
				break;
			case 'G':
				sb.append("01000111");
				break;
			case 'H':
				sb.append("01001000");
				break;
			case 'I':
				sb.append("01001001");
				break;
			case 'J':
				sb.append("01001010");
				break;
			case 'K':
				sb.append("01001011");
				break;
			case 'L':
				sb.append("01001100");
				break;
			case 'M':
				sb.append("01001101");
				break;
			case 'N':
				sb.append("01001110");
				break;
			case 'O':
				sb.append("01001111");
				break;
			case 'P':
				sb.append("10100000");
				break;
			case 'Q':
				sb.append("01010001");
				break;
			case 'R':
				sb.append("01010010");
				break;
			case 'S':
				sb.append("01010011");
				break;
			case 'T':
				sb.append("01010100");
				break;
			case 'U':
				sb.append("01010101");
				break;
			case 'V':
				sb.append("01010110");
				break;
			case 'W':
				sb.append("01010111");
				break;
			case 'X':
				sb.append("01011000");
				break;
			case 'Y':
				sb.append("01011001");
				break;
			case 'Z':
				sb.append("01011010");
				break;
			case '.':
				sb.append("00101110");
				break;
			case ',':
				sb.append("00100111");
				break;
			case ':':
				sb.append("00111010");
				break;
			case ';':
				sb.append("00111011");
				break;
			case '?':
				sb.append("00111111");
				break;
			case '!':
				sb.append("00100001");
				break;
//			case ''':
//				sb.append("00101100");
//				break;
			case '"':
				sb.append("00100010");
				break;
			case '(':
				sb.append("00101000");
				break;
			case ')':
				sb.append("00101001");
				break;
			case ' ':
				sb.append("00100000");
				break;
			case '@':
				sb.append("01000000");
				break;
			case '_':
				sb.append("01011111");
				break;
			case '<':
				sb.append("00111100");
				break;
			case '>':
				sb.append("00111110");
				break;
			case '[':
				sb.append("01011011");
				break;
			case ']':
				sb.append("01011101");
				break;
			}
		}
		System.out.println(sb);
		
	}

	private static String getInput(String prompt) {
		BufferedReader stdin = new BufferedReader(
				new InputStreamReader(System.in));
		
		System.out.print(prompt);
		System.out.flush();
		
		try {
			return stdin.readLine();
		} catch (Exception e) {
			return "Error: " + e.getMessage();
		}
	}
}

Revision: 61737
at January 11, 2013 00:47 by eiger824


Initial Code
package com.santi.encodingtoascii;

import java.io.*;

public class Main {

	public static void main(String[] args) {
		
		String text = getInput("Enter the text to convert to binary code: ");
		System.out.println(text);
		String finalArray="";
		StringBuilder sb = new StringBuilder(finalArray);
		// voy a hacer un bucle que vaya elemento a elemento del array de caracteres
//		y que me lo codifique mediante ascii a binario,
//		luego cada carácter codificado lo meto en un array de números binarios
//		y lo saco por pantalla.
		int len = text.length();
		System.out.println("Length of the text (characters): " +len);
		char [] char_text = text.toCharArray();
		for (int i=0; i<len; i++) {
			char c = char_text[i];
			switch (c) {
			case '0':
				sb.append("00110000");
				break;
			case '1':
				sb.append("00110001");
				break;
			case '2':
				sb.append("00110010");
				break;
			case '3':
				sb.append("00110011");
				break;
			case '4':
				sb.append("00110100");
				break;
			case '5':
				sb.append("00110101");
				break;
			case '6':
				sb.append("00110110");
				break;
			case '7':
				sb.append("00110111");
				break;
			case '8':
				sb.append("00111000");
				break;
			case '9':
				sb.append("00111001");
				break;
			case 'a':
				sb.append("01100001");
				break;
			case 'b':
				sb.append("01100010");
				break;
			case 'c':
				sb.append("01100011");
				break;
			case 'd':
				sb.append("01100100");
				break;
			case 'e':
				sb.append("01100101");
				break;
			case 'f':
				sb.append("01100110");
				break;
			case 'g':
				sb.append("01100111");
				break;
			case 'h':
				sb.append("01101000");
				break;
			case 'i':
				sb.append("01101001");
				break;
			case 'j':
				sb.append("01001010");
				break;
			case 'k':
				sb.append("01101011");
				break;
			case 'l':
				sb.append("01101100");
				break;
			case 'm':
				sb.append("01101101");
				break;
			case 'n':
				sb.append("01101110");
				break;
			case 'o':
				sb.append("01101111");
				break;
			case 'p':
				sb.append("01110000");
				break;
			case 'q':
				sb.append("01110001");
				break;
			case 'r':
				sb.append("01110010");
				break;
			case 's':
				sb.append("01110011");
				break;
			case 't':
				sb.append("01110100");
				break;
			case 'u':
				sb.append("01110101");
				break;
			case 'v':
				sb.append("01110110");
				break;
			case 'w':
				sb.append("01110111");
				break;
			case 'x':
				sb.append("01111000"); 
				break;
			case 'y':
				sb.append("01111001");
				break;
			case 'z':
				sb.append("01111010");
				break;
			case 'A':
				sb.append("01000001");
				break;
			case 'B':
				sb.append("01000010");
				break;
			case 'C':
				sb.append("01000011");
				break;
			case 'D':
				sb.append("01000100");
				break;
			case 'E':
				sb.append("01000101");
				break;
			case 'F':
				sb.append("01000110");
				break;
			case 'G':
				sb.append("01000111");
				break;
			case 'H':
				sb.append("01001000");
				break;
			case 'I':
				sb.append("01001001");
				break;
			case 'J':
				sb.append("01001010");
				break;
			case 'K':
				sb.append("01001011");
				break;
			case 'L':
				sb.append("01001100");
				break;
			case 'M':
				sb.append("01001101");
				break;
			case 'N':
				sb.append("01001110");
				break;
			case 'O':
				sb.append("01001111");
				break;
			case 'P':
				sb.append("10100000");
				break;
			case 'Q':
				sb.append("01010001");
				break;
			case 'R':
				sb.append("01010010");
				break;
			case 'S':
				sb.append("01010011");
				break;
			case 'T':
				sb.append("01010100");
				break;
			case 'U':
				sb.append("01010101");
				break;
			case 'V':
				sb.append("01010110");
				break;
			case 'W':
				sb.append("01010111");
				break;
			case 'X':
				sb.append("01011000");
				break;
			case 'Y':
				sb.append("01011001");
				break;
			case 'Z':
				sb.append("01011010");
				break;
			case '.':
				sb.append("00101110");
				break;
			case ',':
				sb.append("00100111");
				break;
			case ':':
				sb.append("00111010");
				break;
			case ';':
				sb.append("00111011");
				break;
			case '?':
				sb.append("00111111");
				break;
			case '!':
				sb.append("00100001");
				break;
//			case ''':
//				sb.append("00101100");
//				break;
			case '"':
				sb.append("00100010");
				break;
			case '(':
				sb.append("00101000");
				break;
			case ')':
				sb.append("00101001");
				break;
			case ' ':
				sb.append("00100000");
				break;
			case '@':
				sb.append("01000000");
				break;
			case '_':
				sb.append("01011111");
				break;
			case '<':
				sb.append("00111100");
				break;
			case '>':
				sb.append("00111110");
				break;
			case '[':
				sb.append("01011011");
				break;
			case ']':
				sb.append("01011101");
				break;
			}
		}
		System.out.println(sb);
		
	}

	private static String getInput(String prompt) {
		BufferedReader stdin = new BufferedReader(
				new InputStreamReader(System.in));
		
		System.out.print(prompt);
		System.out.flush();
		
		try {
			return stdin.readLine();
		} catch (Exception e) {
			return "Error: " + e.getMessage();
		}
	}
}

Initial URL
http://programmingeiger824.blogspot.com

Initial Description
A simple Text Encoder.

Initial Title
Text-to-Binary Encoder

Initial Tags
java

Initial Language
Java