Advertisement
stephanheijl

Final text2bin code

May 1st, 2013
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package stackoverflowtests;
  2.  
  3. /**
  4.  *
  5.  * @author Stephan
  6.  */
  7. public class StackOverflowTests {
  8.  
  9.     public static void main(String[] args) {
  10.         // This Binary string is bin for 128256128256
  11.         String bin1 = "00111111100011010011111110001101001111111000110100111111100011010011111110001101";
  12.         String text = bin2text(bin1);
  13.         System.out.println(text);
  14.         String bin2 = text2bin(text);
  15.         System.out.println(bin1);
  16.         System.out.println(bin2);
  17.         System.out.println(bin1.equals( bin2 ));
  18.  
  19.     }
  20.  
  21.     /**
  22.      * Binary String to ASCII characters conversion This is an untested example
  23.      * and may or may not work as advertised
  24.      *
  25.      * @author: Stephan Heijl
  26.      */
  27.     public static String bin2text(String binary) {
  28.         int mod = 0; // Indicates the extra padding on the last character
  29.         int pieceNo = (int) (binary.length() / 8); // Number of pieces
  30.         // We'll use StringBuilder as we'll be doing a lot of concatenations
  31.         StringBuilder text = new StringBuilder();
  32.  
  33.         for (int p = 0; p <= pieceNo; p++) {
  34.            
  35.             String piece = "";
  36.             try {
  37.                 piece = binary.substring(p * 8, (p + 1) * 8); // This should be an 8 character long piece of the binary string
  38.             } catch (java.lang.StringIndexOutOfBoundsException e) {
  39.                 // If this is reached the binary string is not a multiple of eight, so we compensate by padding the number with 0's.
  40.                 piece = binary.substring(p * 8); // This might need some work
  41.                 while (piece.length() < 8) {
  42.                     piece = "0" + piece;
  43.                     mod++;
  44.                 }
  45.             }
  46.            
  47.             // First convert the binary string to an int
  48.             int binValue = Integer.parseInt(piece, 2);
  49.             // now convert that to a char
  50.             char binCharacter = (char) binValue;
  51.  
  52.             // And append this to the whole string.
  53.             text.append(String.valueOf(binCharacter));
  54.         }
  55.  
  56.         // Convert the StringBuilder instance into a String to write to a file
  57.         return String.valueOf(mod) + text.toString();
  58.     }
  59.  
  60.     /**
  61.      * Reverse text => Binary String method Also untested, binary to string at:
  62.      * http://pastebin.com/KEYV8jgx
  63.      */
  64.     public static String text2bin(String text) {
  65.         StringBuilder binary = new StringBuilder();
  66.         int mod = Integer.parseInt(text.substring(0,1));
  67.         text = text.substring(1);
  68.        
  69.         for (int c = 0; c < text.length(); c++) {
  70.             // Get the char at each position in the text, cast it to an int
  71.             int n = (int) text.charAt(c);
  72.             // Turn the integer into a binary String (01010101)
  73.             String binString = Integer.toBinaryString(n);
  74.             // Add it to the StringBuilder
  75.  
  76.             while (binString.length() < 8) {
  77.                 binString = "0" + binString;
  78.             }            
  79.             if ( c == text.length()-1 ) {
  80.                 binString = binString.substring(mod);
  81.             }
  82.             binary.append(binString);
  83.         }
  84.         // Convert the StringBuilder instance into a String to decode
  85.         return binary.toString();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement