1. Nzen
  2. 380 4B/5B

Commits

Nzen  committed 927d7d4

Decoding finished

Luis sent me these changes as some error prevented git from pushing. It
seems decoding from 4B5B involves sending the encoded bits to fiveB4B and
passing the decimal bits to decode. The original & resulting bits match,
so feel free to use this naively in the server & client classes.

  • Participants
  • Parent commits 8e25097
  • Branches master

Comments (0)

Files changed (2)

File PhysicalLayer.java

View file
  • Ignore whitespace
 			return Comm.quiet;
 		else if ( bitString.equals( "11111" ) )
 				return Comm.idle;
-			else if ( bitString.equals( "11000" ) )
-					return Comm.startAsk;
-				else if ( bitString.equals( "10001" ) )
-						return Comm.startAck;
-					else if ( bitString.equals( "10011" ) )
-							return Comm.end;
-						else
-							return Comm.NULL; // This wasn't a command, move to decode
+		else if ( bitString.equals( "11000" ) )
+			return Comm.startAsk;
+		else if ( bitString.equals( "10001" ) )
+			return Comm.startAck;
+		else if ( bitString.equals( "10011" ) )
+			return Comm.end;
+		else
+			return Comm.NULL; // This wasn't a command, move to decode
 	}
 	
-	// assuming that client got Comm.Null, decode normally
 	String decode( String fourB5B )
 	{
-		/*		( potential algorithm )
-		 * for bytes in byteBuffer
-		 * 		cast to string or char array
-		 * for chars in bitString
-		 * 		substring in 5 char nibbles
-		 * 		decode via "table"
-		 * 		compute byte value ( perhaps total += 2^digit in nibble )
-		 * 		store high order
-		 * 	 repeat for low order, add to high order
-		 *   cast to char, append to messageBuffer
-		 * return message buffer 
-		 */
-		return "decoding not yet implemented\n";
-	/*
-	( "11110" ); // 0000
-	( "01001" ); // 0001
-	( "10100" ); // 0010
-	( "10101" ); // 0011
-	( "01010" ); // 0100
-	( "01011" ); // 0101
-	( "01110" ); // 0110
-	( "01111" ); // 0111
-	( "10010" ); // 1000
-	( "10011" ); // 1001
-	( "10110" ); // 1010
-	( "10111" ); // 1011
-	( "11010" ); // 1100
-	( "11011" ); // 1101
-	( "11100" ); // 1110
-	( "11101" ); // 1111
-	*/
+		//4B to string
+        String[] str=splitInParts(fourB5B,8);
+        StringBuilder text= new StringBuilder( );
+     
+        for(int a=0;a<str.length;a++)
+           text.append( decoder(str[a]) );
+		   return text.toString( );
 	}
+  	
+	public String fiveB4B(String textE)
+	{
+		//5B to 4B		
+    String[] parts=splitInParts(textE,5);
+    int size=parts.length;			
+    String[] decode= new String[size];
+    String newString="";
+ 		
+    for(int a=0;a<parts.length;a++)
+       decode[a]=fiveB4BD(parts[a]);
+    
+		System.out.print("\n");
+ 						
+    for(int a=0;a<parts.length;a++)
+       newString = newString+decode[a];
+
+    return newString;
+     }
+	   	
+      public String fiveB4BD(String s)
+      {
+         if(s.equals("11110"))
+            return "0000";
+         
+         else if(s.equals("01001"))
+            return "0001";	
+         	
+         else if(s.equals("10100"))
+            return "0010";	
+         	
+         else if(s.equals("10101"))
+            return "0011";	
+         	
+         else if(s.equals("01010"))
+            return "0100";	
+         	
+         else if(s.equals("01011"))
+            return "0101";	
+         	
+         else if(s.equals("01110"))
+            return "0110";
+         
+         else if(s.equals("01111"))
+            return "0111";	
+         	
+         else if(s.equals("10010"))
+            return "1000";	
+         	
+         else if(s.equals("10011"))
+            return "1001";	
+         	
+         else if(s.equals("10110"))
+            return "1010";	
+         	
+         else if(s.equals("10111"))
+            return "1011";	
+         	
+         else if(s.equals("11010"))
+            return "1100";
+         
+         else if(s.equals("11011"))
+            return "1101";	
+         	
+         else if(s.equals("11100"))
+            return "1110";	
+         	
+         else if(s.equals("11101"))
+            return "1111";	
+         else
+        	 return "fail";
+      }
+   	
+    public String[] splitInParts(String s, int partLength)
+    {
+       int len = s.length();
+    
+    // Number of parts
+       int nparts = (len + partLength - 1) / partLength;
+       String parts[] = new String[nparts];
+    
+    // Break into parts
+       int offset= 0;
+       int i = 0;
+       while (i < nparts){
+          parts[i] = s.substring(offset, Math.min(offset + partLength, len));
+          offset += partLength;
+          i++;
+       }
+    
+       return parts;
+    }
+	
+	public String decoder(String str)
+      {
+         int charCode = Integer.parseInt(str, 2);
+         String str2 = new Character((char)charCode).toString();
+         return str2;		
+      }
+	
+	/*	unused method
+    private boolean[] toBinary(int number, int base) {
+        final boolean[] ret = new boolean[base];
+        for (int i = 0; i < base; i++) {
+           ret[base - 1 - i] = (1 << i & number) != 0;
+        }
+        return ret;
+     } */
 }

File TestPhysical.java

View file
  • Ignore whitespace
 		PhysicalLayer link = new PhysicalLayer( );
 		
 		System.out.println( "\tTesting output" );
-		testOutput( link );
+		String bits = testOutput( link );
 
 		System.out.println( "\tTesting input" );
 		pretestInput( link );
-		testInput( link );
+		testInput( link, bits );
 	}
 	
-	static void testOutput( PhysicalLayer link )
+	static String testOutput( PhysicalLayer link )
 	{
 		String output;
 		String message = "/>M\\kz!0HY";
 		System.out.println( "in hex: 2F 3E 4D 5C 6B 7A 21 30 48 59" );
 		output = link.encode( message );
 		System.out.printf( "Encoded bits: %s\n", output );
-		// Actually produces codes matching 3F 3E 4D 5C 6B 7A 31 30 48 59 // circa 12 4 23
+		// Produces codes matching 2F 3E 4D 5C 6B 7A 31 30 48 59
+		return output;
 	}
 	
 	static void pretestInput( PhysicalLayer link )
 		}
 	}
 	
-	static void testInput( PhysicalLayer link )
+	static void testInput( PhysicalLayer link, String inputBits )
 	{
-		String message = link.decode( "01110" ); // replace
+		String decBits=link.fiveB4B( inputBits );
+		System.out.printf( "Decoded bits: %s\n", decBits );
+		String text=link.decode(decBits);
+		System.out.printf( "Text Decoded bits: %s\n", text );
 	}
 	/*0	( "11110" ) 	1	( "01001" ) 	2	( "10100" ) 	3	( "10101" )
 	4	( "01010" ) 	5	( "01011" ) 	6	( "01110" ) 	7	( "01111" )